(function() {
    "use strict";

    /**
     * @ngdoc directive
     * @name webApp.directive:itemList
     * @description
     * # itemList
     */
    angular
        .module("webApp")
        .directive("itemList", function() {
            return {
                restrict: "E",
                templateUrl: "views/directives.item.list.html",
                scope: {},
                controller: [
                    "$templateCache",
                    "$rootScope",
                    "$scope",
                    "$compile",
                    "dataService",
                    "$timeout",
                    "sku",
                    "helper",
                    function(
                        $templateCache,
                        $rootScope,
                        $scope,
                        $compile,
                        dataService,
                        $timeout,
                        sku,
                        helper
                    ) {
                        //1、事件 2、function 函数接受参数
                        $rootScope.$on("event:item-list", function(event, data) {
                            $scope.activity_id = data.activity_id;
                            $scope.pageData(1);
                            // console.log("activity_id--->", $scope.activity_id);
                        });

                        //请求数据
                        $scope.itemsTotal = 0;
                        $scope.pageSize = 5;
                        $scope.pageData = function(pageNo) {
                            $scope.page_no = pageNo;
                            var query = {};
                            query.query = {
                                activity_id: $scope.activity_id
                            };
                            dataService.dazheGet(query).then(function(result) {
                                //参加该活动的商品
                                var itemData = {};
                                var activity_items = result.activitys[0] ?
                                    result.activitys[0].activity_items : {};

                                async.map(
                                    activity_items,
                                    function(item, call) {
                                        if (itemData[item.item_id]) {
                                            var sku_id = item.sku_id;
                                            itemData[item.item_id][sku_id] = item;
                                        } else {
                                            var sku_id = item.sku_id;
                                            itemData[item.item_id] = {};
                                            itemData[item.item_id][sku_id] = {};
                                            itemData[item.item_id][sku_id] = item;
                                        }
                                        call(null, itemData);
                                    },
                                    function(e, r) {
                                        var checked_item_id = _.keys(itemData);
                                        var a = {
                                            page_no: pageNo ? pageNo : "1",
                                            page_size: $scope.pageSize
                                        };
                                        var subquery = {};
                                        subquery.store_id = $rootScope.store_id;

                                        if ($scope.keyword) {
                                            // subquery.title = $scope.keyword;
                                            subquery = {
                                                "store_id": $rootScope.store_id,
                                                "title.icontains": $scope.keyword
                                            };
                                        }
                                        a.query = subquery;

                                        dataService.itemGet(a).then(function(itemList) {
                                            $scope.itemslist = [];

                                            if (itemList) {
                                                async.mapSeries(
                                                    itemList.items,
                                                    function(item, call) {
                                                        //判断这个商品ID，是否在参加活动的数组中 !=-1 选中
                                                        var item_id = item.item_id;
                                                        item.dazhe = {};
                                                        if (
                                                            checked_item_id.indexOf(item_id) != -1 &&
                                                            _.size(checked_item_id)
                                                        ) {
                                                            item.dazhe = {};
                                                            var item_id = item.item_id;
                                                            if (itemData[item_id][0]) {
                                                                item.dazhe.id = itemData[item_id][0].id;
                                                                item.dazhe.discount =
                                                                    itemData[item_id][0].discount;
                                                                item.dazhe.disprice =
                                                                    itemData[item_id][0].disprice;
                                                                item.dazhe.type_id =
                                                                    itemData[item_id][0].type_id;
                                                                item.dazhe.id = itemData[item_id][0].id;
                                                                item.dazhe.item_checked = "item";
                                                                item.dazhe.dazhe_type = "item";

                                                                var itemSkus = $scope.updateSkuType(item);
                                                                item.newSkus = sku.skuRuCan(itemSkus);
                                                                item.otherSkus = sku.addDazheOtherSkus(
                                                                    item.skus
                                                                );
                                                            } else {
                                                                item.dazhe.item_checked = "sku";
                                                                item.dazhe.dazhe_type = "sku";

                                                                var itemSkus = $scope.updateSkuType(item);
                                                                item.newSkus = sku.skuRuCan(itemSkus);
                                                                item.otherSkus = sku.addDazheOtherSkus(
                                                                    item.skus,
                                                                    itemData[item_id]
                                                                );
                                                            }

                                                            //赋值原价
                                                            item.yuan_price = item.price;

                                                            $scope.itemslist.push(item);
                                                        } else {
                                                            item.dazhe.discount = 100;
                                                            item.dazhe.disprice = 0;
                                                            item.dazhe.type_id = 1;
                                                            item.dazhe.item_checked = "item";

                                                            var itemSkus = $scope.updateSkuType(item);
                                                            item.newSkus = sku.skuRuCan(itemSkus);
                                                            item.otherSkus = sku.addDazheOtherSkus(item.skus);

                                                            //赋值原价
                                                            item.yuan_price = item.price;

                                                            $scope.itemslist.push(item);
                                                        }
                                                        call(null, null);
                                                    },
                                                    function(e, r) {
                                                        // console.log("sdlkfjsdlfksjdlfkjowr230r932", $scope.itemslist);
                                                    }
                                                );
                                            }
                                            $scope.itemsTotal = itemList.total_count;
                                        });
                                    }
                                );
                            });
                        };

                        //检查打折 减钱是否合法
                        $scope.checkLegality = function(item, flag) {
                            $scope.$watch(
                                "item",
                                function(newValue, oldValue, scope) {
                                    if (newValue) {
                                        if (flag == "item") {
                                            if (item.dazhe.type_id == 1) {
                                                if (
                                                    item.dazhe.discount <= 0 ||
                                                    item.dazhe.discount > 100 ||
                                                    isNaN(item.dazhe.discount)
                                                ) {
                                                    helper.myTip(
                                                        "error:请正确输入商品折扣",
                                                        "#btn-" + item.item_id
                                                    );
                                                    return false;
                                                }
                                            } else {
                                                if (
                                                    item.dazhe.disprice <= 0 ||
                                                    isNaN(item.dazhe.disprice)
                                                ) {
                                                    helper.myTip(
                                                        "error:请正确输入商品折扣",
                                                        "#btn-" + item.item_id
                                                    );
                                                    return false;
                                                }
                                            }
                                        }

                                        if (flag == "sku") {
                                            for (var i in item.otherSkus) {
                                                var discount = Number(item.otherSkus[i].dazhe.discount);
                                                var disprice = Number(item.otherSkus[i].dazhe.disprice);

                                                if (item.otherSkus[i].dazhe.type_id == 1) {
                                                    if (
                                                        item.otherSkus[i].dazhe.discount <= 0 ||
                                                        item.otherSkus[i].dazhe.discount > 100 ||
                                                        isNaN(item.otherSkus[i].dazhe.discount)
                                                    ) {
                                                        helper.myTip(
                                                            "error:请正确输入商品折扣",
                                                            "#sku-" + item.item_id
                                                        );
                                                        return false;
                                                    }
                                                } else {
                                                    if (
                                                        item.otherSkus[i].dazhe.disprice <= 0 ||
                                                        isNaN(item.otherSkus[i].dazhe.disprice)
                                                    ) {
                                                        helper.myTip(
                                                            "error:请正确输入商品折扣",
                                                            "#sku-" + item.item_id
                                                        );
                                                        return false;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },
                                true
                            );
                        };

                        //商品打折
                        $scope.addItemCuxiao = function(item, flag) {
                            if ($scope.activity_id) {
                                item.activity_id = $scope.activity_id;
                            }

                            if (flag == "item") {
                                var checkedItem = {
                                    partner_id: 1,
                                    activity_id: Number(item.activity_id),
                                    item_id: item.item_id,
                                    type_id: item.dazhe.type_id,
                                    discount: Number(item.dazhe.discount),
                                    disprice: Number(item.dazhe.disprice)
                                };

                                //判断价格是否合法
                                if (checkedItem.type_id == 1) {
                                    if (
                                        checkedItem.discount <= 0 ||
                                        checkedItem.discount > 100 ||
                                        isNaN(checkedItem.discount)
                                    ) {
                                        helper.myTip(
                                            "error:请正确输入商品折扣",
                                            "#btn-" + item.item_id
                                        );
                                        return false;
                                    }
                                } else {
                                    if (
                                        checkedItem.disprice <= 0 ||
                                        isNaN(checkedItem.disprice)
                                    ) {
                                        helper.myTip(
                                            "error:请正确输入商品折扣",
                                            "#btn-" + item.item_id
                                        );
                                        return false;
                                    }
                                }

                                var a = [];
                                a.push(checkedItem);
                            } else {
                                var a = [];
                                for (var i in item.otherSkus) {
                                    var sku = {
                                        partner_id: 1,
                                        activity_id: Number(item.activity_id),
                                        item_id: item.item_id,
                                        sku_id: item.otherSkus[i].sku_id,
                                        type_id: item.otherSkus[i].dazhe.type_id,
                                        discount: Number(item.otherSkus[i].dazhe.discount),
                                        disprice: Number(item.otherSkus[i].dazhe.disprice)
                                    };

                                    if (sku.type_id == 1) {
                                        if (
                                            sku.discount <= 0 ||
                                            sku.discount > 100 ||
                                            isNaN(sku.discount)
                                        ) {
                                            helper.myTip(
                                                "error:请正确输入商品折扣",
                                                "#sku-" + item.item_id
                                            );
                                            return false;
                                        }
                                    } else {
                                        if (sku.disprice <= 0 || isNaN(sku.disprice)) {
                                            helper.myTip(
                                                "error:请正确输入商品折扣",
                                                "#sku-" + item.item_id
                                            );
                                            return false;
                                        }
                                    }

                                    a.push(sku);
                                }
                            }
                            var data = {};
                            data.activity_id = $scope.activity_id;
                            data.activity_items = a;

                            dataService.addItemCuxiao(data).then(function(result) {
                                if (result.activity_id && flag == "item") {
                                    $scope.DisableItem(item.item_id, flag, item);
                                } else if (result.activity_id && flag == "sku") {
                                    $scope.DisableItem(item.item_id, flag, item);
                                } else {}

                                // var query = {
                                //     content: {
                                //         store_id: $rootScope.store_id,
                                //         item_id: item.item_id,
                                //         dazhe_type: "part"
                                //     }
                                // };
                                // $scope.nsqrequest(query);

                                $scope.pageData($scope.page_no);
                            });
                            return false;

                            //商品打折之前先判断商品是不是参加过其他的活动
                            var query = {
                                item_id: item.item_id
                            };

                            dataService.actItemGet(query).then(function(act_item) {
                                var itemData = {};
                                _.map(act_item.activity_items, function(item, k) {
                                    if (itemData[item.item_id]) {
                                        var sku_id = item.sku_id;
                                        itemData[item.item_id][sku_id] = item;
                                    } else {
                                        var sku_id = item.sku_id;
                                        itemData[item.item_id] = {};
                                        itemData[item.item_id][sku_id] = {};
                                        itemData[item.item_id][sku_id] = item;
                                    }
                                });

                                var item_id = item.item_id;
                                if (
                                    _.keys(itemData).indexOf(item_id) != -1 &&
                                    _.size(_.keys(itemData))
                                ) {
                                    helper.myAlert("error:该商品已经参加过其他打折活动");
                                    return false;
                                } else {
                                    dataService.addItemCuxiao(data).then(function(result) {
                                        if (result.activity_id && flag == "item") {
                                            $scope.DisableItem(item.item_id, flag, item);
                                        } else if (result.activity_id && flag == "sku") {
                                            $scope.DisableItem(item.item_id, flag, item);
                                        } else {}

                                        // var query = {
                                        //     content: {
                                        //         store_id: $rootScope.store_id,
                                        //         item_id: item.item_id,
                                        //         dazhe_type: "part"
                                        //     }
                                        // };
                                        // $scope.nsqrequest(query);

                                        $scope.pageData($scope.page_no);
                                    });
                                }
                            });
                        };

                        $scope.nsqrequest = function(data) {
                            dataService.nsqRequest(data).then(function(result) {
                                // console.error("result-->", result);
                            });
                        };

                        //商品退出打折
                        $scope.removeItemCuxiao = function(item, activity_id, id, flag) {
                            if (!id) {
                                helper.myAlert("商品移除错误，返回列表重新进入，移除");
                                return false;
                            }

                            helper.myConfirm("确认删除吗？", function() {
                                if (flag == "sku") {
                                    var othersku = {};
                                    for (var i in item.otherSkus) {
                                        othersku[item.otherSkus[i].sku_id] = item.otherSkus[i];
                                    }
                                    if (othersku) {
                                        async.mapSeries(
                                            othersku,
                                            function(sku, call) {
                                                if (sku.dazhe) {
                                                    var iid = sku.dazhe.id;
                                                    var query = {
                                                        id: iid,
                                                        activity_id: $scope.activity_id
                                                    };
                                                    dataService
                                                        .removeItemCuxiao(query)
                                                        .then(function(data) {
                                                            if (data.Err) {
                                                                helper.myAlert("error" + data.Msg);
                                                                return false;
                                                            }
                                                            call(null, item);
                                                        });
                                                }
                                            },
                                            function(e, r) {
                                                $scope.EnableItem(item, "sku");
                                                $scope.pageData($scope.page_no);
                                                // var query1 = {
                                                //     content: {
                                                //         store_id: $rootScope.store_id,
                                                //         item_id: item.item_id,
                                                //         dazhe_type: "part",
                                                //         delete: 1
                                                //     }
                                                // };
                                                // $scope.nsqrequest(query1);
                                            }
                                        );
                                    }
                                } else {
                                    var query = {
                                        id: id,
                                        activity_id: $scope.activity_id
                                    };
                                    dataService.removeItemCuxiao(query).then(function(data) {
                                        if (data.Err) {
                                            helper.myAlert("error" + data.Msg);
                                        } else {
                                            $scope.EnableItem(item, flag);
                                            $scope.pageData($scope.page_no);

                                            // var query = {
                                            //     content: {
                                            //         store_id: $rootScope.store_id,
                                            //         item_id: item.item_id,
                                            //         dazhe_type: "part",
                                            //         delete: 1
                                            //     }
                                            // };
                                            // $scope.nsqrequest(query);
                                        }
                                    });
                                }
                            });
                        };

                        //激活1个商品
                        $scope.EnableItem = function(item, flag) {
                            if (flag == "item") {
                                item.dazhe.item_checked = "item";
                            } else {
                                item.dazhe.item_checked = "sku";
                            }

                            item.dazhe.dazhe_type = "";
                        };

                        //禁止1个商品
                        $scope.DisableItem = function(num_iid, flag, item) {
                            if (flag == "item") {
                                item.dazhe.dazhe_type = "item";
                            } else {
                                item.dazhe.dazhe_type = "sku";
                            }
                        };

                        //设置框显示与隐藏 默认隐藏选中商品之后显示 $event,
                        // 多选框选中的时候，直接给商品打折，否则就是sku打折
                        $scope.showInput = function(num_iid, item) {
                            if ($("#item_" + num_iid).val() == "-") {
                                item.dazhe.item_checked = "item";
                            } else {
                                item.dazhe.item_checked = "sku";
                            }
                        };

                        //批量输入框的显示隐藏
                        $scope.freezeBtn = function() {
                            $scope.selectedItemCount();
                            var checkNum = $(".numiidary:checked").length;
                            var statu = checkNum > 0 ? true : false;

                            if (statu) {
                                $("#piLiang").removeClass("zhide");
                            } else {
                                $("#piLiang").addClass("zhide");
                            }
                        };

                        //显示选中了几个商品
                        $scope.selectedItemCount = function() {
                            var checkNum = $(".numiidary:checked").length;
                            $(".selectedItemCount").html(checkNum);
                        };

                        //设置商品的type
                        $scope.setType = function(item1, v) {
                            item1.dazhe.type_id = v;
                            console.error("item---", item1);
                            $scope.item = item1;
                        };

                        //设置sku的type
                        $scope.setSkuType = function(item2, v, id) {
                            item2.otherSkus[id].dazhe.type_id = v;
                            $scope.item = item2;
                        };

                        //分解sku 组装淘宝格式 显示
                        $scope.updateSkuType = function(itemData) {
                            var fgeoneDate = splitAry(itemData.skus);
                            if (fgeoneDate) {
                                var arr = oneConTwo(fgeoneDate); //将一维数组 转换有父子关系的二维数组
                                var aryData = unique5(arr); //去除重复值；
                                if (aryData) {
                                    var newata = [];
                                    _.map(aryData, function(dd, kk) {
                                        newata.push({
                                            pid: dd.pid,
                                            name: dd.name,
                                            values: []
                                        });
                                        var len = dd.values;
                                        for (var j = 0; j < len.length; j++) {
                                            if (len[j]) {
                                                newata[kk].values.push(len[j]);
                                            }
                                            if (len.length == j + 1) {
                                                newata[kk].values.push({
                                                    id: 0,
                                                    value: "",
                                                    mark: ""
                                                });
                                            }
                                        }
                                    });
                                    return newata;
                                    $scope.skuNew = newata;
                                    console.error("skuNew---->", $scope.skuNew);
                                }
                            }
                        };

                        //拆分，切割数组，组成需要是一维数组
                        function splitAry(skus) {
                            var skusDateAry = [];
                            _.map(skus, function(da, k) {
                                skusDateAry.push({
                                    data: da.sku_properties
                                });
                            });
                            var fgeoneDate = [];
                            _.map(skusDateAry, function(dss, kk) {
                                var strs = dss.data.split(";");
                                strs = strs.filter(function(n) {
                                    return n;
                                });
                                if (strs) {
                                    _.map(strs, function(dd, kk) {
                                        var svbtrs = dd.split(":");
                                        if (svbtrs[0] && svbtrs[2] && svbtrs[1] && svbtrs[3]) {
                                            fgeoneDate.push({
                                                pid: Number(svbtrs[0]),
                                                name: svbtrs[2],
                                                id: Number(svbtrs[1]),
                                                value: svbtrs[3]
                                            });
                                        }
                                    });
                                }
                            });
                            // console.log("装的数据", fgeoneDate);
                            return fgeoneDate;
                        }

                        //将多个带有父子关系的一维数组，转换为二维数组
                        function oneConTwo(fgeoneDate) {
                            var map = {};
                            var arr = [];
                            for (var i = 0; i < fgeoneDate.length; i++) {
                                var di = fgeoneDate[i];
                                var n = di.pid;
                                if (!map[n]) {
                                    //先判断 pid是否有只 有值 先给父级赋值
                                    map[n] = di.name;
                                    arr.push({
                                        pid: n,
                                        name: di.name,
                                        values: [{
                                            id: di.id,
                                            value: di.value
                                        }]
                                    });
                                } else {
                                    for (var j = 0; j < arr.length; j++) {
                                        var aj = arr[j];
                                        if (aj.pid == n) {
                                            //pid相等的时候，在给子类赋值
                                            arr[j].values.push({
                                                id: di.id,
                                                value: di.value
                                            });
                                            break;
                                        }
                                    }
                                }
                            }
                            return arr;
                        }

                        //去除二维数组中 重复 的一维数组
                        function unique5(arrays) {
                            _.map(arrays, function(arra, k) {
                                var array = arra.values;
                                for (var i = 0, l = array.length; i < l; i++) {
                                    for (var j = i + 1; j < l; j++) {
                                        //判断 第一个和第二个数组 内容是否相同 ，id相同 就代表数据相同
                                        if (array[i].id === array[j].id) {
                                            delete arra.values[i];
                                            j = ++i;
                                        }
                                    }
                                }
                            });
                            return arrays;
                        }
                    }
                ]
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.choiceitem.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("choiceItem", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{isChoseItem?'chose_item':'chose_item_sku' | translate}}</a> ",
                scope: {
                    radioItem: "=",
                    chosedItem: "=",
                    mult: "@",
                    itemCheck: "&",
                    itemslist: "@",
                    isChoseItem: "@", //是否选择商品，而不是选择sku
                    shopitem: "@",
                    outHtml: "@",
                    skushow: "@",
                    num: "@"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.items = [];
                        $scope.chosedItems = [];
                        $scope.ismult = $scope.mult == "yes" ? true : false;
                        $scope.chosedIndex = null;
                        $rootScope.$on("event:up-store-id", function() {
                            $scope.pageData();
                        });

                        $scope.addItem = function() {
                            $scope.pageData().then(showPop);
                        };

                        function showPop() {
                            $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: '{{"Item" | translate}}',
                                body: $templateCache.get("views/directives.choiceitem.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.chosedItems.length > 0) {
                                            // $scope.ngModer = $scope.chosedItems;
                                            $scope.itemCheck({
                                                items: $scope.ismult ?
                                                    $scope.chosedItems : $scope.chosedItems[0]
                                            });
                                            // console.log("$scope.chosedItems", $scope.chosedItems);
                                        }
                                    });
                                }
                            });
                        }

                        $scope.isChecked = function(item, sku) {
                            item.sku_data = sku;
                            $scope.chosedItems = [item];

                            _.map($scope.items, function(d, k) {
                                if (d.item_id == item.item_id) {
                                    item.type = "radio";
                                } else {
                                    d.type = "";
                                }
                            });
                            // console.error("itemitem", item);
                            if ($scope.outHtml) {
                                $timeout(function() {
                                    if ($scope.chosedItems.length > 0) {
                                        $scope.itemCheck({
                                            items: $scope.ismult ?
                                                $scope.chosedItems : $scope.chosedItems[0]
                                        });
                                    }
                                });
                            }
                        };

                        //请求数据
                        $scope.itemsTotal = 0;
                        $scope.pageSize = 10;
                        $scope.pageData = function(pageNo) {
                            pageNo = pageNo ? pageNo : 1;
                            var query = {
                                page_no: pageNo,
                                page_size: $scope.pageSize
                            };
                            query.query = {
                                store_id: $rootScope.store_id,
                                on_shelves: 1
                            };

                            if ($scope.outer_id) {
                                query.query.outer_id = $scope.outer_id;
                            }
                            if ($scope.keyword) {
                                query.name = $scope.keyword;
                            }
                            return dataService
                                .itemGet(query, $scope.keyword)
                                .then(function(data) {
                                    $scope.itemSku = {};
                                    $scope.itemData = [];

                                    _.map(data.items, function(a) {
                                        var itemdata = {
                                            title: a.title,
                                            item_id: a.item_id,
                                            img: a.img,
                                            price: a.price,
                                            fare_id: a.fare_id,
                                            note: a.note,
                                            is_incl_postage: a.is_incl_postage,
                                            sale_num: a.sale_num
                                        };
                                        if (!$scope.skushow) {
                                            itemdata.skus = a.skus;
                                        }

                                        if (a.skus) {
                                            a.len = a.skus.length;
                                            _.map(a.skus, function(b) {
                                                $scope.itemSku[a.item_id] = clone(itemdata);
                                                // $scope.itemSku[a.item_id + "_" + b.sku_id] = clone(a);
                                                // $scope.itemSku[a.item_id + "_" + b.sku_id]['sku'] = b;
                                            });
                                        }

                                        if ($scope.radioItem) {
                                            if (a.item_id == $scope.radioItem) {
                                                itemdata.type = "radio";
                                            }
                                        }

                                        // console.error("$scope.num", a.skus, $scope.itemSku);
                                        if ($scope.num && a.skus) {
                                            if (a.skus.length == 1) {
                                                $scope.itemData.push(itemdata);
                                            }
                                        } else {
                                            $scope.itemData.push(itemdata);
                                        }
                                    });
                                    $scope.items = $scope.itemData;
                                    $scope.itemsTotal = data.total_count;
                                    if ($scope.chosedItem) {
                                        if ($scope.chosedItem.length > 0) {
                                            $scope.chosedItems = $scope.chosedItem;
                                        }
                                    }
                                });
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {
                    if (scope.outHtml) {} else {
                        scope.class = attrs.xclass;
                    }
                }
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.choicepage.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("choicePage", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: "<a href='javascript:void(0);'   ng-click='addItem();'>+{{'促销活动' | translate}}</a> ",
                scope: {
                    itemCheck: "&",
                    wxappid: "@"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.addItem = function() {
                            $scope.pageData().then(showPop);
                        };

                        function showPop() {
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: '{{"活动" | translate}}',
                                body: $templateCache.get("views/directives.choicepage.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.page) {
                                            $scope.itemCheck({
                                                page: $scope.page
                                            });
                                        }
                                    });
                                }
                            });
                        }

                        $scope.isChecked = function(item, sku) {
                            // console.error("pageidpageid", item);
                            $scope.page = item;
                        };

                        //请求数据
                        $scope.activity_Total = 0;
                        $scope.pageSize = 10;
                        $scope.pageData = function(pageNo) {
                            var query = {
                                page_no: pageNo ? pageNo : "1",
                                page_size: $scope.pageSize
                            };
                            query.query = {
                                app_id: $scope.wxappid
                            };

                            if ($scope.title) {
                                query.query.title = $scope.title;
                            }
                            return dataService.activitypageGet(query).then(function(r) {
                                if (!r.Msg) {
                                    $scope.activitys = r.activity_pages;
                                    $scope.activity_Total = r.total_count;
                                }
                            });
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .directive("itemName", function() {
            return {
                restrict: "E",
                template: '<span ng-if="level_name" style="width:130px;margin-bottom:5px;">{{level_name}}<button class="sui-btn btn-small btn-danger" ng-if="ngModel" ng-click="clearLevel()">X</button></span><span ng-if="huodong_name"><span style="display:block;width:130px;"><img ng-src="{{huodong_img}}" width="150"/></span><span style="display:block;width:130px;">{{huodong_name}}</span></span><span ng-if="item_name"><span style="display:block;width:130px;"><img ng-src="{{item_img}}" width="150"/></span><span style="display:block;width:130px;">{{item_name}}</span></span>',
                // replace: true,
                // require: 'ngModel',
                scope: {
                    itemId: "=",
                    skuId: "=",
                    type: "=",
                    ngModel: "=",
                    memberId: "="
                },
                controller: [
                    "$templateCache",
                    "$scope",
                    "dataService",
                    "$compile",
                    "$timeout",
                    "$rootScope",
                    "helper",
                    function(
                        $templateCache,
                        $scope,
                        dataService,
                        $compile,
                        $timeout,
                        $rootScope,
                        helper
                    ) {
                        $scope.initData = function() {
                            if ($scope.type == "huodong") {
                                var query = {
                                    content: {
                                        store_id: $rootScope.store_id,
                                        huodong_id: $scope.item_id
                                    }
                                };
                                dataService.huodongGet(query).then(function(list) {
                                    if (list) {
                                        $scope.huodong_name = list.huodong_list[0].name;
                                        $scope.huodong_img = list.huodong_list[0].img;
                                    }
                                });
                            } else if ($scope.type == "level") {
                                if ($scope.ngModel) {
                                    var query = {};
                                    query.query = {
                                        member_level_id: $scope.ngModel
                                    };
                                    dataService.memberlevelGet(query).then(function(list) {
                                        if (list.member_levels.length > 0) {
                                            $scope.member_level = list.member_levels[0];
                                            $scope.level_name = $scope.member_level.name;
                                        }
                                    });
                                } else {
                                    $scope.level_name = "无";
                                }

                                $scope.clearLevel = function() {
                                    helper.myConfirm("确认清除会员等级吗？", function() {
                                        var data = {
                                            member_id: $scope.memberId,
                                            level: 0
                                        };
                                        dataService
                                            .phpMemberlevelUpdate(data)
                                            .then(function(result) {
                                                if (result.Msg != "ok") {
                                                    helper.myAlert("error：" + result.Msg);
                                                    return false;
                                                } else {
                                                    helper.message("修改成功！");
                                                    $scope.level_name = "无";
                                                    $scope.ngModel = 0;
                                                }
                                            });
                                    });
                                };
                            } else {
                                if ($scope.skuId) {
                                    dataService
                                        .itemSkuOne($scope.itemId, $scope.skuId)
                                        .then(function(item_data) {
                                            if (item_data) {
                                                $scope.item_name = item_data.title;
                                                $scope.item_img = item_data.sku.imgurl;
                                            }
                                        });
                                } else {
                                    dataService.itemOne($scope.itemId).then(function(item_data) {
                                        if (item_data) {
                                            $scope.item_name = item_data.title;
                                            $scope.item_img = item_data.img;
                                        }
                                    });
                                }
                            }
                        };
                    }
                ],
                link: function(scope, element, attrs, ctrl) {
                    scope.initData();
                    scope.$watch("ngModel", function(newValue, oldValue, scope) {
                        if (newValue && newValue != oldValue) {
                            scope.initData();
                        }
                    });
                }
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.choicecoupon.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("choiceCoupon", function(
            $templateCache,
            $compile,
            $timeout,
            helper,
            sessionService
        ) {
            return {
                restrict: "EA",
                template: "<a href='javascript:void(0);' ng-click='addItem1();'>+{{'卡券列表' | translate}}</a> ",
                scope: {
                    itemCheck: "&",
                    wxappid: "@"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    "helper",
                    function($rootScope, $scope, dataService, helper) {
                        $scope.addItem1 = function() {
                            // $scope.pageData($scope.wxappid, 0).then(showPop);

                            showPop();
                        };

                        function showPop() {
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: '{{"卡券" | translate}}',
                                body: $templateCache.get("views/directives.choicecoupon.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.page) {
                                            $scope.itemCheck({
                                                page: $scope.page
                                            });
                                        }
                                    });
                                }
                            });
                        }

                        $scope.isChecked = function(item, sku) {
                            console.error("pageidpageid", item);
                            $scope.page = item;
                        };

                        //请求数据
                        $scope.total_num = 0;
                        $scope.pageSize = 10;
                        $scope.pageData = function(app_id, pageNo) {
                            var tokenUser = sessionService.get("tokenUser");
                            var partner_id = tokenUser.CompanyId;

                            var data = {
                                method: "dhf.card.list.get",
                                content: {
                                    gzh_appid: app_id,
                                    page_size: $scope.pageSize,
                                    page_no: pageNo
                                }
                            };

                            // console.error("data-->", data);
                            // return false;
                            dataService.cardListGet(data).then(function(result) {
                                if (result.total_count) {
                                    $scope.card_list = result.card_list;
                                    $scope.total_count = result.total_count;
                                    $scope.authorizer_appid = app_id;
                                } else {
                                    helper.myAlert("error: 去卡券页面下载卡券再来选择！");
                                }
                            });

                            // var data = {
                            //     "req_method": "POST",
                            //     "authorizer_appid": app_id,
                            //     "url_prefix": "https://api.weixin.qq.com/card/batchget?access_token=TOKEN",
                            //     "content": { offset: pageNo, count: $scope.pageSize }
                            // }

                            // //console.error("sdlkfjsdf", app_id);

                            // return dataService.card(data).then(function(result) {
                            //     console.error("卡券列表--->", result, result.errmsg);

                            //     if (result.errcode == 0 && result.errmsg == "ok") {
                            //         console.error("-------------------------------", result.card_id_list);
                            //         $scope.card_id_list = result.card_id_list;
                            //         $scope.total_num = result.total_num;

                            //         $scope.authorizer_appid = app_id;
                            //     } else {
                            //         console.error("error: " + result.errmsg);
                            //     }
                            // });
                        };

                        $scope.formatDate = function(now) {
                            return new Date(parseInt(now) * 1000)
                                .toLocaleString()
                                .replace(/:\d{1,2}$/, " ");
                        };
                        $scope.cardType = function(card_type) {
                            switch (card_type) {
                                case "GROUPON":
                                    return {
                                        type: "groupon",
                                        name: "团购券"
                                    };
                                    break;

                                case "DISCOUNT":
                                    return {
                                        type: "discount",
                                        name: "折扣券"
                                    };
                                    break;

                                case "GIFT":
                                    return {
                                        type: "gift",
                                        name: "礼品券"
                                    };
                                    break;

                                case "CASH":
                                    return {
                                        type: "cash",
                                        name: "代金券"
                                    };
                                    break;

                                case "GENERAL_COUPON":
                                    return {
                                        type: "general_coupon",
                                        name: "通用券"
                                    };
                                    break;

                                case "MEMBER_CARD":
                                    return {
                                        type: "member_card",
                                        name: "会员卡"
                                    };
                                    break;

                                case "SCENIC_TICKET":
                                    return {
                                        type: "scenic_ticket",
                                        name: "景点门票"
                                    };
                                    break;

                                case "MOVIE_TICKET":
                                    return {
                                        type: "movie_ticket",
                                        name: "电影票"
                                    };
                                    break;

                                case "BOARDING_PASS":
                                    return {
                                        type: "boarding_pass",
                                        name: "飞机票"
                                    };
                                    break;

                                case "MEETING_TICKET":
                                    return {
                                        type: "meeting_ticket",
                                        name: "会议门票"
                                    };
                                    break;

                                case "BUS_TICKET":
                                    return {
                                        type: "bus_ticket",
                                        name: "骑车票"
                                    };
                                    break;
                            }
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .directive("couponInfo", function(
            $templateCache,
            $compile,
            $timeout,
            helper
        ) {
            return {
                restrict: "EA",
                templateUrl: "views/directives.couponinfo.html",
                scope: {
                    wxappid: "@",
                    cardid: "@"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    "helper",
                    function($rootScope, $scope, dataService, helper) {
                        //请求数据
                        $scope.pageData = function() {
                            var data = {
                                req_method: "POST",
                                authorizer_appid: $scope.wxappid,
                                url_prefix: "https://api.weixin.qq.com/card/get?access_token=TOKEN",
                                content: {
                                    card_id: $scope.cardid
                                }
                            };

                            //console.error("sdkfjsdlkfjsdlfkj23423", data);

                            dataService.card(data).then(function(detailData) {
                                console.error("卡券详情数据--->", detailData);
                                // return false;
                                $scope.card_info = {};

                                if (detailData.errcode == 0 && detailData.errmsg == "ok") {
                                    var card_type = $scope.cardType(detailData.card.card_type);
                                    var type1 = card_type.type;
                                    var info = detailData.card[type1];

                                    $scope.card_info = detailData.card[type1].base_info;

                                    $scope.card_info.type_name = card_type.name;
                                    // var s_time = new Date($scope.card_info.date_info.begin_timestamp);
                                    $scope.card_info.start_time = $scope.formatDate(
                                        $scope.card_info.date_info.begin_timestamp
                                    );
                                    // var e_time = new Date($scope.card_info.date_info.end_timestamp);
                                    $scope.card_info.end_time = $scope.formatDate(
                                        $scope.card_info.date_info.end_timestamp
                                    );

                                    //固定日期
                                    if (
                                        info.base_info.date_info.type == "DATE_TYPE_FIX_TIME_RANGE"
                                    ) {
                                        // info.date_info.end_timestamp
                                        $scope.card_info.start_time = $scope.formatDate(
                                            $scope.card_info.date_info.begin_timestamp
                                        );
                                        $scope.card_info.end_time = $scope.formatDate(
                                            $scope.card_info.date_info.end_timestamp
                                        );

                                        $scope.card_info.time_range = true;
                                    }

                                    //固定时长
                                    if (info.base_info.date_info.type == "DATE_TYPE_FIX_TERM") {
                                        // info.date_info.end_timestamp
                                        $scope.card_info.fixed_term =
                                            $scope.card_info.date_info.fixed_term;
                                        $scope.card_info.fixed_begin_term =
                                            $scope.card_info.date_info.fixed_begin_term;

                                        $scope.card_info.fix_term = true;
                                    }

                                    $scope.card_info.status_name = $scope.statusName(
                                        $scope.card_info.status
                                    );
                                    $scope.card_info.card_id = $scope.card_id;

                                    console.error("card_info---->", $scope.card_info);
                                } else {
                                    console.error("卡券详情错误-->", detailData.errmsg);
                                    helper.myAlert(detailData.errmsg);
                                }
                            });
                        };

                        $scope.statusName = function(status) {
                            switch (status) {
                                case "CARD_STATUS_NOT_VERIFY":
                                    return "待审核";

                                case "CARD_STATUS_VERIFY_FAIL":
                                    return "审核失败";

                                case "CARD_STATUS_VERIFY_OK":
                                    return "待投放";

                                case "CARD_STATUS_DELETE":
                                    return "已删除";

                                case "CARD_STATUS_DISPATCH":
                                    return "已投放";
                            }
                        };

                        $timeout(function() {
                            $scope.pageData();
                        });

                        $scope.formatDate = function(now) {
                            return new Date(parseInt(now) * 1000)
                                .toLocaleString()
                                .replace(/:\d{1,2}$/, " ");
                        };
                        $scope.cardType = function(card_type) {
                            switch (card_type) {
                                case "GROUPON":
                                    return {
                                        type: "groupon",
                                        name: "团购券"
                                    };
                                    break;

                                case "DISCOUNT":
                                    return {
                                        type: "discount",
                                        name: "折扣券"
                                    };
                                    break;

                                case "GIFT":
                                    return {
                                        type: "gift",
                                        name: "礼品券"
                                    };
                                    break;

                                case "CASH":
                                    return {
                                        type: "cash",
                                        name: "代金券"
                                    };
                                    break;

                                case "GENERAL_COUPON":
                                    return {
                                        type: "general_coupon",
                                        name: "通用券"
                                    };
                                    break;

                                case "MEMBER_CARD":
                                    return {
                                        type: "member_card",
                                        name: "会员卡"
                                    };
                                    break;

                                case "SCENIC_TICKET":
                                    return {
                                        type: "scenic_ticket",
                                        name: "景点门票"
                                    };
                                    break;

                                case "MOVIE_TICKET":
                                    return {
                                        type: "movie_ticket",
                                        name: "电影票"
                                    };
                                    break;

                                case "BOARDING_PASS":
                                    return {
                                        type: "boarding_pass",
                                        name: "飞机票"
                                    };
                                    break;

                                case "MEETING_TICKET":
                                    return {
                                        type: "meeting_ticket",
                                        name: "会议门票"
                                    };
                                    break;

                                case "BUS_TICKET":
                                    return {
                                        type: "bus_ticket",
                                        name: "骑车票"
                                    };
                                    break;
                            }
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.chosemember.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("choseMember", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{titles | translate}}</a> ",
                scope: {
                    wxappid: "@",
                    mult: "@",
                    memberCheck: "&",
                    title: "@",
                    app: "@",
                    chosedItems: "="
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.items = [];
                        // $scope.chosedItems = [];
                        $scope.ismult = $scope.mult == "yes" ? true : false;
                        $scope.chosedIndex = null;
                        $scope.titles = $scope.title ? $scope.title : "手动添加代理商";

                        $scope.addItem = function() {
                            $scope.pageData().then(showPop);
                        };

                        function showPop() {
                            $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                            // console.error("chosedItems--->", $scope.chosedItems);
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: "{{titles | translate}}",
                                body: $templateCache.get("views/directives.chosemember.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.ismult) {
                                            if ($scope.chosedItems.length) {
                                                $scope.memberCheck({
                                                    memberInfo: $scope.chosedItems
                                                });
                                            }
                                        } else {
                                            if ($scope.memberInfo.nick_name) {
                                                $scope.memberCheck({
                                                    memberInfo: $scope.memberInfo
                                                });
                                            }
                                        }
                                    });
                                }
                            });
                        }

                        $scope.isChecked = function(user) {
                            $scope.memberInfo = user;
                        };

                        //h回调
                        $scope.callback = function(app_id) {
                            if (app_id) {
                                $scope.wxappid = app_id;
                                $scope.pageData();
                            }
                        };

                        //请求数据
                        $scope.pageSize = 10;
                        $scope.pageData = function(pageNo) {
                            pageNo = pageNo ? pageNo : 1;
                            var query = {
                                page_no: pageNo,
                                page_size: $scope.pageSize
                            };

                            var subquery = {};
                            if ($scope.keyword) {
                                subquery = {
                                    "name.icontains": $scope.keyword
                                };
                            }

                            if ($scope.phone) {
                                subquery.phone = $scope.phone;
                            }

                            subquery.app_id = $scope.wxappid;
                            query.query = subquery;

                            return dataService
                                .memberGet(query, $scope.keyword)
                                .then(function(data) {
                                    if (data.wx_members) {
                                        $scope.users = data.wx_members;
                                        $scope.total_count = data.total_count;
                                        console.error("checked user-->", $scope.users);
                                    }
                                });
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .directive("catitemsList", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                templateUrl: "views/directives.catitems_list.html",
                scope: {
                    ngModel: "="
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.card = $scope.ngModel;

                        //请求数据
                        $scope.initactive = function(cid) {
                            if (cid == $scope.intcid) {
                                return cid;
                            }
                        };

                        //请求数据
                        $scope.pageData = function(pageNo) {
                            var query = {};

                            $scope.store = $rootScope.store_id;
                            if ($scope.store) {
                                query.query = {
                                    store_id: $scope.store
                                };
                                query.page_size = 100;
                            }

                            dataService.categoryGet(query).then(function(list) {
                                if (list.itemcats) {
                                    var listdata = list.itemcats;
                                    $scope.itemcats = [];

                                    _.map(listdata, function(a, i) {
                                        if (listdata[i].parent_cid !== 0) {
                                            if (i == 1) {
                                                $scope.intcid = listdata[i].cid;
                                                $scope.ischeck(listdata[i].cid);
                                            }

                                            $scope.itemcats.push({
                                                id: listdata[i].cid,
                                                name: listdata[i].name
                                            });
                                        }
                                    });
                                }
                            });
                        };

                        $scope.ischeck = function(cid) {
                            console.log("$scope.itemcats", $scope.itemcats, cid);

                            var query = {
                                page_size: 3,
                                query: {
                                    category_id: cid,
                                    store_id: $scope.store
                                }
                            };

                            dataService.itemGet(query).then(function(list) {
                                if (list) {
                                    $scope.itemsTotal = list.total_count;
                                    $scope.itemslist = list.items;
                                    console.log("cid", $scope.itemslist);
                                }
                            });
                        };

                        $scope.pageData();
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .directive("catitemsUdcid", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                templateUrl: "views/directives.catitems_udcid.html",
                scope: {
                    ngModel: "="
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.$watch(
                            "ngModel",
                            function(newValue, oldValue, scope) {
                                console.log("ngModel", $scope.ngModel);
                                if (newValue && oldValue != newValue) {
                                    $scope.init();
                                }
                            },
                            true
                        );

                        $scope.pageData = function() {
                            $timeout(function() {
                                $scope.store = $rootScope.store_id;

                                var query = {
                                    query: {
                                        category_id: $scope.ngModel.data[1].value,
                                        store_id: $scope.store
                                    }
                                };
                                dataService.itemGet(query).then(function(list) {
                                    if (list) {
                                        $scope.subdata.value = list.items;
                                        console.log("subdata", $scope.subdata);
                                    }
                                });
                            });
                        };

                        $scope.init = function() {
                            $scope.subdata = [];
                            //                             console.error("$scope.ngModel", $scope.ngModel.data[1]);
                            $timeout(function() {
                                if ($scope.ngModel.data[1].value) {
                                    $scope.pageData();
                                    $scope.card = $scope.ngModel;
                                }
                            });
                        };

                        $scope.init();
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })

    .directive("catrentUdcid", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            templateUrl: "views/directives.catitems_udcid.html",
            scope: {
                ngModel: "=",
                type: "@"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.$watch(
                        "ngModel",
                        function(newValue, oldValue, scope) {
                            console.log("ngModel", $scope.ngModel);
                            if (newValue && oldValue != newValue) {
                                $scope.init();
                            }
                        },
                        true
                    );

                    $scope.pageData = function() {
                        $timeout(function() {
                            $scope.store = $rootScope.store_id;

                            var query = {
                                query: {
                                    category_id: $scope.ngModel.data[1].value,
                                    store_id: $scope.store
                                }
                            };
                            dataService.itemGet(query).then(function(list) {
                                if (list) {
                                    $scope.subdata.value = list.items;
                                    console.log("subdata", $scope.subdata);
                                }
                            });
                        });
                    };

                    $scope.init = function() {
                        $scope.subdata = [];
                        //                             console.error("$scope.ngModel", $scope.ngModel.data[1]);
                        $timeout(function() {
                            if ($scope.ngModel.data[1].value) {
                                $scope.pageData();
                                $scope.card = $scope.ngModel;
                            }
                        });
                    };

                    $scope.init();
                }
            ],

            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .directive("catItems", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            templateUrl: "views/directives.catitems.html",
            scope: {
                ngModel: "="
            },

            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.card = $scope.ngModel;

                    $scope.$watch(
                        "ngModel",
                        function(newValue, oldValue, scope) {
                            if (newValue && oldValue != newValue) {
                                $scope.init();
                            }
                        },
                        true
                    );

                    //请求数据
                    $scope.initactive = function(cid) {
                        if (cid == $scope.intcid) {
                            return cid;
                        }
                    };

                    $scope.init = function() {
                        console.error("$scope.ngModel", $scope.ngModel);
                        $timeout(function() {
                            if ($scope.ngModel.data[5].value) {
                                $scope.catlists = $scope.ngModel.data[5].value;
                                $scope.card = $scope.ngModel;
                                _.map($scope.catlists, function(a, i) {
                                    if (i == 0) {
                                        $scope.intcid = a.cid;
                                        $scope.ischeck(a.cid);
                                    }
                                });
                            }
                        });
                    };

                    $scope.ischeck = function(cid) {
                        $scope.store = $rootScope.store_id;
                        console.log("$scope.itemcats", $scope.itemcats, cid);
                        var query = {
                            page_size: 3,
                            query: {
                                category_id: cid,
                                store_id: $scope.store
                            }
                        };
                        $scope.subdata = {
                            value: {}
                        };
                        dataService.itemGet(query).then(function(list) {
                            if (list) {
                                $scope.subdata.value = list.items;
                                console.log("cid", $scope.subdata);
                            }
                        });
                    };

                    $scope.init();
                }
            ],

            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.choseuser.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("choseUser", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{'手动添加卖家' | translate}}</a> ",
            scope: {
                mult: "@",
                memberCheck: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.items = [];
                    $scope.chosedItems = [];
                    $scope.ismult = $scope.mult == "yes" ? true : false;
                    $scope.chosedIndex = null;

                    $scope.addItem = function() {
                        $scope.pageData().then(showPop);
                    };

                    function showPop() {
                        $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"手动添加卖家" | translate}}',
                            body: $templateCache.get("views/directives.choseuser.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.memberInfo.nick_name) {
                                        $scope.memberCheck({
                                            memberInfo: $scope.memberInfo
                                        });
                                    }
                                });
                            }
                        });
                    }

                    $scope.isChecked = function(user) {
                        // console.error("checked user-->", user);
                        $scope.memberInfo = user;
                    };

                    //请求数据
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        var query = {
                            page_no: pageNo,
                            page_size: $scope.pageSize
                        };

                        return dataService.sellerGet(query).then(function(data) {
                            if (data) {
                                $scope.users = data.users;
                                $scope.total_count = data.total_count;
                            }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .directive("pickpointInfo", function() {
        return {
            restrict: "E",
            template: '<span ng-if="address&&phone" title="地址：{{address}}\n电话：{{phone}}"><i class="sui-icon icon-tb-locationfill" title="发货点"></i> {{name}}</span>',
            require: "ngModel",
            scope: {
                ngModel: "="
            },
            controller: [
                "$templateCache",
                "$scope",
                "dataService",
                "$compile",
                "$timeout",
                function($templateCache, $scope, dataService, $compile, $timeout) {
                    $scope.ngModel = $scope.ngModel || 0;
                    $scope.initData = function() {
                        if ($scope.ngModel && $scope.ngModel != 0) {
                            dataService
                                .pickpointGet({
                                    content: {
                                        pickpoint_id: $scope.ngModel
                                    }
                                })
                                .then(function(result) {
                                    if (result.total_count > 0) {
                                        var pickpoint = result.pickpoint_list[0];
                                        $scope.address =
                                            pickpoint.province +
                                            " " +
                                            pickpoint.city +
                                            " " +
                                            pickpoint.district +
                                            " " +
                                            pickpoint.address;
                                        $scope.phone = pickpoint.phone;
                                        $scope.name = pickpoint.name;
                                    }
                                });
                        }
                    };
                }
            ],
            link: function(scope, element, attrs, ctrl) {
                ctrl.$render = function() {
                    if (ctrl.$viewValue) {
                        scope.initData();
                    }
                };
            }
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.choselucky.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("choseLucky", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{'选择抽奖活动' | translate}}</a> ",
            scope: {
                mult: "@",
                memberCheck: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.items = [];
                    $scope.chosedItems = [];
                    $scope.ismult = $scope.mult == "yes" ? true : false;
                    $scope.chosedIndex = null;

                    $scope.addItem = function() {
                        $scope.pageData().then(showPop);
                    };

                    function showPop() {
                        $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"选择抽奖活动" | translate}}',
                            body: $templateCache.get("views/directives.choselucky.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.memberInfo.name) {
                                        $scope.memberCheck({
                                            actInfo: $scope.memberInfo
                                        });
                                    }
                                });
                            }
                        });
                    }

                    $scope.isChecked = function(user) {
                        // console.error("checked user-->", user);
                        $scope.memberInfo = user;
                    };

                    //请求数据
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        var query = {
                            content: {
                                store_id: $rootScope.store_id,
                                page_no: pageNo ? pageNo : "1",
                                page_size: $scope.pageSize
                            }
                        };

                        return dataService.luckydrawGet(query).then(function(data) {
                            if (data) {
                                $scope.draw_list = data.draw_list;
                                $scope.total_count = data.total_count;
                            }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .directive("chooseItem", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            templateUrl: "views/directives.chooseitem.html",
            scope: {
                radioItem: "=",
                chosedItem: "=",
                mult: "@",
                itemCheck: "&",
                itemslist: "@",
                isChoseItem: "@", //是否选择商品，而不是选择sku
                shopitem: "@",
                outHtml: "@",
                skushow: "@",
                num: "@"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.items = [];
                    $scope.chosedItems = [];
                    $scope.ismult = $scope.mult == "yes" ? true : false;
                    $scope.chosedIndex = null;

                    $rootScope.$on("event:choose-item", function(event, data) {
                        $scope.pageData(1);
                    });

                    $scope.isChecked = function() {
                        $timeout(function() {
                            if ($scope.chosedItems.length > 0) {
                                $scope.itemCheck({
                                    items: $scope.ismult ?
                                        $scope.chosedItems : $scope.chosedItems[0]
                                });
                            }
                        });
                    };

                    //请求数据
                    $scope.itemsTotal = 0;
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        var query = {
                            page_no: pageNo,
                            page_size: $scope.pageSize
                        };
                        query.query = {
                            store_id: $rootScope.store_id,
                            on_shelves: 1
                        };

                        if ($scope.keyword) {
                            query.name = $scope.keyword;
                        }

                        if ($scope.outer_id) {
                            query.query = {
                                outer_id: $scope.outer_id
                            };
                        }

                        return dataService
                            .itemGet(query, $scope.keyword)
                            .then(function(data) {
                                $scope.itemSku = {};
                                $scope.itemData = [];

                                _.map(data.items, function(a) {
                                    var itemdata = {
                                        title: a.title,
                                        item_id: a.item_id,
                                        img: a.img,
                                        price: a.price,
                                        fare_id: a.fare_id,
                                        note: a.note,
                                        is_incl_postage: a.is_incl_postage,
                                        sale_num: a.sale_num
                                    };
                                    if (!$scope.skushow) {
                                        itemdata.skus = a.skus;
                                    }

                                    if (a.skus) {
                                        a.len = a.skus.length;
                                        _.map(a.skus, function(b) {
                                            $scope.itemSku[a.item_id] = clone(itemdata);
                                            // $scope.itemSku[a.item_id + "_" + b.sku_id] = clone(a);
                                            // $scope.itemSku[a.item_id + "_" + b.sku_id]['sku'] = b;
                                        });
                                    }

                                    if ($scope.radioItem) {
                                        if (a.item_id == $scope.radioItem) {
                                            itemdata.type = "radio";
                                        }
                                    }

                                    // console.error("$scope.num", a.skus, $scope.itemSku);
                                    if ($scope.num && a.skus) {
                                        if (a.skus.length == 1) {
                                            $scope.itemData.push(itemdata);
                                        }
                                    } else {
                                        $scope.itemData.push(itemdata);
                                    }
                                });
                                $scope.items = $scope.itemData;
                                $scope.itemsTotal = data.total_count;
                                if ($scope.chosedItem) {
                                    if ($scope.chosedItem.length > 0) {
                                        $scope.chosedItems = $scope.chosedItem;
                                    }
                                }
                            });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {
                if (scope.outHtml) {} else {
                    scope.class = attrs.xclass;
                }
            }
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.chosepickpoint.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("chosePickpoint", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>{{title}}</a> ",
            scope: {
                title: "@",
                chosedItems: "=", //选择的数据
                memberCheck: "&",
                type: "@"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.items = [];

                    $scope.ismult = $scope.mult == "yes" ? true : false;

                    $scope.addItem = function() {
                        $scope.pageData().then(showPop);
                    };

                    //单选
                    $scope.checkedRadio = function(id) {
                        if (id) {
                            $scope.chosedItems = id;
                            console.error("$scope.chosedItems", $scope.chosedItems);
                        }
                    };

                    function showPop() {
                        if ($scope.type) {
                            $scope.chosedItems = $scope.chosedItems ?
                                $scope.chosedItems :
                                0;
                        } else {
                            $scope.chosedItems = $scope.chosedItems ?
                                $scope.chosedItems : [];
                        }
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: "{{title | translate}}",
                            body: $templateCache.get(
                                "views/directives.chosepickpoint.html"
                            ),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    console.error("$scope.chosedItems", $scope.chosedItems);
                                    if ($scope.chosedItems && $scope.memberCheck) {
                                        // $scope.backcall();
                                        $scope.memberCheck({
                                            pickpoint: $scope.chosedItems
                                        });
                                    }
                                });
                            }
                        });
                    }

                    //请求数据
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        var query = {
                            content: {
                                page_no: pageNo ? pageNo : "1",
                                page_size: $scope.pageSize,
                                store_id: $rootScope.store_id
                            }
                        };

                        return dataService.pickpointGet(query).then(function(list) {
                            if (list && list.pickpoint_list) {
                                $scope.pickpoint_list = list.pickpoint_list;
                                $scope.total_count = list.total_count;
                            }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {
                scope.class = attrs.xclass;
            }
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.gzhsctag.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("gzhscTag", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{title}}</a> ",
            scope: {
                title: "@",
                chosedItems: "=", //选择的数据
                memberCheck: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.addItem = function() {
                        $scope.pageData().then(showPop);
                    };

                    function showPop() {
                        $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: "{{title | translate}}",
                            body: $templateCache.get("views/directives.gzhsctag.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.chosedItems && $scope.memberCheck) {
                                        console.error("$scope.chosedItems", $scope.chosedItems);
                                        $scope.memberCheck({
                                            tag: $scope.chosedItems
                                        });
                                    }
                                });
                            }
                        });
                    }

                    //请求数据
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        var query = {
                            content: {
                                page_no: pageNo ? pageNo : "1",
                                page_size: $scope.pageSize
                            }
                        };

                        return dataService.gzhsutagGet(query).then(function(list) {
                            if (list.gzhsc_tag_list) {
                                $scope.gzhsc_tag_list = list.gzhsc_tag_list;
                                $scope.total_count = list.total_count;
                            }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {
                scope.class = attrs.xclass;
            }
        };
    })

    .directive("gzhsctagInfo", function() {
            return {
                restrict: "E",
                template: '<span ng-if="name">{{name}}</span>',
                require: "ngModel",
                scope: {
                    ngModel: "="
                },
                controller: [
                    "$templateCache",
                    "$scope",
                    "dataService",
                    "$compile",
                    "$timeout",
                    function($templateCache, $scope, dataService, $compile, $timeout) {
                        $scope.ngModel = $scope.ngModel || 0;
                        $scope.initData = function() {
                            if ($scope.ngModel) {
                                dataService
                                    .gzhsutagGet({
                                        content: {
                                            gzhsc_tag_id: $scope.ngModel
                                        }
                                    })
                                    .then(function(result) {
                                        if (result.total_count > 0) {
                                            var ngModel = result.gzhsc_tag_list[0];
                                            $scope.name = ngModel.name;
                                        }
                                    });
                            }
                        };
                    }
                ],
                link: function(scope, element, attrs, ctrl) {
                    ctrl.$render = function() {
                        if (ctrl.$viewValue) {
                            scope.initData();
                        }
                    };
                }
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.updateorderpickpoint.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("updateOrderPickpoint", function(
            $templateCache,
            $compile,
            $timeout
        ) {
            return {
                restrict: "EA",
                template: "<a href='javascript:void(0);' class='sui-btn btn-warning'  ng-click='addItem();'>{{title}}</a> ",
                scope: {
                    title: "@",
                    orderId: "=",
                    chosedPid: "=",
                    memberCheck: "&"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.ismult = $scope.mult == "yes" ? true : false;

                        $scope.addItem = function() {
                            $scope.pageData().then(showPop);
                        };

                        function showPop() {
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: "{{title | translate}}",
                                body: $templateCache.get(
                                    "views/directives.updateorderpickpoint.html"
                                ),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.chosedItems && $scope.memberCheck) {
                                            console.error("11122")
                                            $scope.memberCheck({
                                                pickpoint: {
                                                    pickpoint_id: $scope.chosedItems.pickpoint_id,
                                                    order_id: $scope.orderId
                                                }
                                            });
                                        }
                                    });
                                }
                            });
                        }

                        //请求数据
                        $scope.pageSize = 10;
                        $scope.pageData = function(pageNo) {
                            var query = {
                                content: {
                                    page_no: pageNo ? pageNo : "1",
                                    page_size: $scope.pageSize,
                                    store_id: $rootScope.store_id
                                }
                            };

                            return dataService.pickpointGet(query).then(function(list) {
                                if (list && list.pickpoint_list) {
                                    $scope.pickpoint_list = list.pickpoint_list;
                                    $scope.total_count = list.total_count;
                                }
                            });
                        };

                        $scope.isChecked = function(chosedItems) {
                            $timeout(function() {
                                $scope.chosedItems = chosedItems;
                            });
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {
                    scope.class = attrs.xclass;
                }
            };
        })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.updateorderwuliu.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("updateOrderWuliu", function(
        $templateCache,
        $compile,
        $timeout
    ) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='sui-btn btn-warning'  ng-click='showyuouwuliu();'>请选择物流</a> ",
            require: 'ngModel',
            scope: {
                ngModel: "=",
                title: "@",
                orderId: "=",
            },
            controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout) {

                $scope.showyuouwuliu = function() {
                    $scope.getwuliu().then(showPop);
                };

                function showPop() {
                    $.alert({
                        backdrop: "static",
                        keyboard: true,
                        title: "{{title | translate}}",
                        body: $templateCache.get(
                            "views/directives.updateorderwuliu.html"
                        ),
                        okBtn: '{{"ok" | translate}}',
                        closeBtn: true,
                        transition: true,
                        width: 500,
                        height: 300,
                        show: function(e) {
                            $compile($(e.currentTarget).contents())($scope);
                            return true;
                        },
                        okHide: function(e) {
                            $timeout(function() {
                                $scope.confirmWuliu();
                            });
                        }
                    });
                }

                //保存渝欧订单物流信息
                $scope.confirmWuliu = function() {
                    $scope.configData.orderNo = $scope.orderId;
                    dataService.confirmWuliu($scope.configData).then(function(r) {
                        if (r) {
                            if (r.errCode == "success") {
                                helper.message(r.msg);
                            } else {
                                helper.message(r.msg, "error");
                            }
                        }
                    });
                }

                $scope.getwuliu = function() {
                    return dataService.yuouWuliu().then(function(list) {
                        $scope.wuliu = list.data;
                    });
                }
            }],
        };
    })

    .directive("confessionDetailInfo", function() {
        return {
            restrict: "E",
            template: '<span ng-if="confession_copy">{{confession_copy}}<br></span>',
            require: "ngModel",
            scope: {
                ngModel: "="
            },
            controller: [
                "$templateCache",
                "$scope",
                "dataService",
                "$compile",
                "$timeout",
                "$rootScope",
                function(
                    $templateCache,
                    $scope,
                    dataService,
                    $compile,
                    $timeout,
                    $rootScope
                ) {
                    $scope.ngModel = $scope.ngModel || 0;
                    $scope.initData = function() {
                        if ($scope.ngModel) {
                            dataService
                                .confessionDetailGet({
                                    content: {
                                        store_id: $rootScope.store_id,
                                        detail_id: $scope.ngModel
                                    }
                                })
                                .then(function(result) {
                                    if (result.total_count > 0) {
                                        var detailData = result.detail_list[0];
                                        $scope.confession_copy = detailData.confession_copy;
                                    }
                                });
                        }
                    };
                }
            ],
            link: function(scope, element, attrs, ctrl) {
                ctrl.$render = function() {
                    if (ctrl.$viewValue) {
                        scope.initData();
                    }
                };
            }
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.choosecoupon.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("chooseCoupon", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: "<a href='javascript:void(0);' class='{{class}}' ng-click='addItem();'>+{{'选择优惠券' | translate}}</a> ",
                scope: {
                    ngModel: "=",
                    radioItem: "=",
                    chosedItem: "=",
                    mult: "@",
                    itemCheck: "&",
                    itemslist: "@",
                    isChoseItem: "@", //是否选择商品，而不是选择sku
                    shopitem: "@",
                    outHtml: "@",
                    skushow: "@",
                    num: "@",
                    callback: "&"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    "helper",
                    function($rootScope, $scope, dataService, helper) {
                        $scope.items = [];
                        $scope.chosedItems = $scope.ngModel ? $scope.ngModel : [];
                        $scope.ismult = $scope.mult == "yes" ? true : false;
                        $scope.chosedIndex = null;
                        $rootScope.$on("event:up-store-id", function() {
                            $scope.pageData();
                        });

                        $scope.addItem = function() {
                            $scope.pageData().then(showPop);
                        };

                        function showPop() {
                            $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: '{{"优惠券" | translate}}',
                                body: $templateCache.get("views/directives.choosecoupon.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.ismult) {
                                            if ($scope.chosedItems.length > 0) {
                                                $scope.ngModel = $scope.chosedItems;
                                            }
                                        } else {
                                            if ($scope.couponInfo) {
                                                $scope.ngModel = $scope.couponInfo.mjs_activity_id;
                                            }
                                            if ($scope.callback) {
                                                $scope.callback({
                                                    mjs_activity_id: $scope.couponInfo.mjs_activity_id
                                                });
                                            }
                                        }
                                    });
                                }
                            });
                        }

                        $scope.isChecked = function(info) {
                            $scope.couponInfo = info;
                        };

                        //请求数据
                        $scope.itemsTotal = 0;
                        $scope.pageSize = 10;
                        $scope.pageData = function(pageNo) {
                            pageNo = pageNo ? pageNo : 1;

                            var query = {
                                content: {
                                    page_no: pageNo,
                                    page_size: $scope.pageSize,
                                    store_id: $rootScope.store_id,
                                    h_type: "coupon"
                                }
                            };

                            return dataService.mjsGet(query).then(function(list) {
                                if (list) {
                                    $scope.mjs_list = list.mjs_list;
                                    $scope.total_count = list.total_count;

                                    if ($scope.chosedItem) {
                                        if ($scope.chosedItem.length > 0) {
                                            $scope.chosedItems = $scope.chosedItem;
                                        }
                                    }
                                }
                            });
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {
                    if (scope.outHtml) {} else {
                        scope.class = attrs.xclass;
                    }
                }
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.choosetab.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])

    .directive("chooseTab", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}' ng-click='addItem();'>+{{'选择标签' | translate}}</a> ",
            scope: {
                ngModel: "=",
                radioItem: "=",
                chosedItem: "=",
                mult: "@",
                itemCheck: "&",
                itemslist: "@",
                isChoseItem: "@", //是否选择商品，而不是选择sku
                shopitem: "@",
                outHtml: "@",
                skushow: "@",
                num: "@",
                callback: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                "helper",
                function($rootScope, $scope, dataService, helper) {
                    $scope.items = [];
                    $scope.chosedItems = $scope.ngModel ? $scope.ngModel : [];
                    $scope.ismult = $scope.mult == "yes" ? true : false;
                    $scope.chosedIndex = null;
                    $rootScope.$on("event:up-store-id", function() {
                        $scope.pageData();
                    });

                    $scope.addItem = function() {
                        $scope.pageData().then(showPop);
                    };

                    function showPop() {
                        $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"标签" | translate}}',
                            body: $templateCache.get("views/directives.choosetab.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.ismult) {
                                        if ($scope.chosedItems.length > 0) {
                                            $scope.ngModel = $scope.chosedItems;
                                        }
                                    } else {
                                        if ($scope.labelInfo) {
                                            $scope.ngModel = $scope.labelInfo.label_id;
                                        }
                                        if ($scope.callback) {
                                            $scope.callback({
                                                label_id: $scope.labelInfo.label_id
                                            });
                                        }
                                    }
                                });
                            }
                        });
                    }

                    $scope.isChecked = function(info) {
                        $scope.labelInfo = info;
                    };

                    //请求数据
                    $scope.itemsTotal = 0;
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;

                        var query = {
                            page_no: pageNo,
                            page_size: $scope.pageSize,
                            store_id: $rootScope.store_id
                        };

                        return dataService.labelGet(query).then(function(list) {
                            if (list.label_list) {
                                $scope.label_list = list.label_list;
                                $scope.total_count = list.total_count;
                            }

                            //     if ($scope.chosedItem) {
                            //         if ($scope.chosedItem.length > 0) {
                            //             $scope.chosedItems = $scope.chosedItem
                            //         }
                            //     }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {
                if (scope.outHtml) {} else {
                    scope.class = attrs.xclass;
                }
            }
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.chose.freight.rule.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("choseFreightRule", function(
            $templateCache,
            $compile,
            $timeout
        ) {
            return {
                restrict: "EA",
                template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{titles | translate}}</a> ",
                scope: {
                    freightCheck: "&",
                    title: "@",
                    chosedItems: "="
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.items = [];

                        $scope.chosedIndex = null;
                        $scope.titles = $scope.title ? $scope.title : "选择阶梯运费";

                        $scope.addItem = function() {
                            $scope.pageData().then(showPop);
                        };

                        function showPop() {
                            $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                            // console.error("chosedItems--->", $scope.chosedItems);
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: "{{titles | translate}}",
                                body: $templateCache.get(
                                    "views/directives.chose.freight.rule.html"
                                ),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.ruleInfo) {
                                            $scope.freightCheck({
                                                ruleInfo: $scope.ruleInfo
                                            });
                                        }
                                    });
                                }
                            });
                        }

                        $scope.isChecked = function(ruleInfo) {
                            console.error("ruleInfo--->", ruleInfo);
                            $scope.ruleInfo = ruleInfo;
                        };

                        //请求数据
                        $scope.pageSize = 10;
                        $scope.pageData = function(pageNo) {
                            pageNo = pageNo ? pageNo : 1;
                            var query = {
                                page_no: pageNo,
                                page_size: $scope.pageSize
                            };

                            var subquery = {};

                            subquery.store_id = $rootScope.store_id;
                            query.query = subquery;

                            return dataService.stepfreightGet(query).then(function(list) {
                                if (list.freight_rule) {
                                    $scope.freight_rule = list.freight_rule;
                                    $scope.total_count = list.total_count;
                                }
                            });
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.choseagent.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("choseAgent", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{titles | translate}}</a> ",
                scope: {
                    memberCheck: "&"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.titles = $scope.title ? $scope.title : "选择代理商";
                        $scope.addItem = function() {
                            $scope.pageData().then(showPop);
                        };

                        function showPop() {
                            $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: "{{titles | translate}}",
                                body: $templateCache.get("views/directives.choseagent.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.memberInfo) {
                                            if ($scope.memberCheck) {
                                                $scope.memberCheck({
                                                    agentInfo: $scope.memberInfo.user_id
                                                });
                                            }
                                        }
                                    });
                                }
                            });
                        }

                        $scope.isChecked = function(user) {
                            $scope.memberInfo = user;
                        };

                        //请求数据
                        $scope.pageSize = 10;
                        $scope.pageData = function(pageNo) {
                            var query = {
                                content: {
                                    page_no: pageNo ? pageNo : "1",
                                    page_size: $scope.pageSize,
                                    store_id: $rootScope.store_id,
                                    member_id: $scope.member_id ? $scope.member_id : 0
                                }
                            };
                            return dataService.distributorGet(query).then(function(data) {
                                if (data.distributor_list) {
                                    $scope.users = data.distributor_list;
                                    $scope.total_count = data.total_count;
                                    console.error("checked user-->", $scope.users);
                                }
                            });
                        };

                        $scope.memberdata1 = function(memberInfo) {
                            $scope.member_id = memberInfo.member_id;
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.chosetemplate.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("choseTemplate", function(
        $templateCache,
        $compile,
        $timeout,
        helper
    ) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{'模板库' | translate}}</a> ",
            scope: {
                titleCheck: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.titles = $scope.title ? $scope.title : "模板库";
                    $scope.addItem = function() {
                        $scope.pageData().then(showPop);
                    };

                    function showPop() {
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: "{{titles | translate}}",
                            body: $templateCache.get("views/directives.chosetemplate.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                if ($scope.keyword) {
                                    console.log("选中的模板", $scope.keyword);
                                    $scope.titleCheck({
                                        title: $scope.keyword
                                    });
                                }
                            }
                        });
                    }

                    //请求数据
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        var query = {
                            pageNo: pageNo ? pageNo : "1",
                            pageSize: $scope.pageSize
                        };

                        return dataService.titleLibraryGet(query).then(function(data) {
                            if (data.Msg == "ok") {
                                $scope.title_list = data.list;
                                $scope.total_count = data.total_count;
                            } else {
                                helper.myAlert("error：" + data.Msg);
                                return false;
                            }
                        });
                    };

                    $scope.keywordCheck = function(keyword) {
                        console.error("keyword", keyword);
                        // return false;
                        $scope.keyword = keyword;
                        $scope.tid = keyword.tid;
                        $scope.chosed_keywords = keyword.chosed_keywords;
                    };
                }
            ],
            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.templatelibrary.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("templateLibrary", function(
        $templateCache,
        $compile,
        $timeout,
        helper
    ) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{'公共模板库' | translate}}</a> ",
            scope: {
                appId: "=",
                titleCheck: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.titles = $scope.title ? $scope.title : "公共模板库";
                    $scope.addItem = function() {
                        console.error("dddddd", $scope.appId);

                        $scope.pageData().then(function(result) {
                            if (result) {
                                $scope.showPop();
                            }
                        });
                    };

                    $scope.showPop = function() {
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: "{{titles | translate}}",
                            body: $templateCache.get("views/directives.templatelibrary.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                if ($scope.keyword) {
                                    console.log("选中的模板", $scope.keyword);
                                    $scope.titleCheck({
                                        title: $scope.keyword
                                    });
                                }
                            }
                        });
                    }

                    //请求数据
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        if (!$scope.appId) {
                            helper.myAlert("没有选择小程序，无法查看公共模板库");
                            return false;
                        }
                        var query = {
                            pageNo: pageNo ? pageNo : "1",
                            pageSize: $scope.pageSize,
                            appId: $scope.appId,
                        };

                        return dataService.templateTitleListGet(query).then(function(data) {
                            if (data.Msg == "ok") {
                                $scope.title_list = data.list;
                                $scope.total_count = data.total_count;
                                return true;
                            } else {
                                helper.myAlert("error：" + data.Msg);
                                return false;
                            }
                        });
                    };

                    $scope.keywordCheck = function(keyword) {
                        console.error("keyword", keyword);
                        // return false;
                        $scope.keyword = keyword;
                        $scope.tid = keyword.tid;
                        $scope.chosed_keywords = keyword.chosed_keywords;
                    };
                }
            ],
            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.chosekeyword.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("choseKeyword", function(
        $templateCache,
        $compile,
        $timeout,
        helper
    ) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{'关键词库' | translate}}</a> ",
            scope: {
                ngModel: "=",
                title: "=",
                keywordCheck: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.titles = $scope.title ? $scope.title : "关键词库";
                    $scope.addItem = function() {
                        $scope.pageData().then(showPop);
                    };

                    function showPop() {
                        $scope.chosedKeywords = [];
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: "{{titles | translate}}",
                            body: $templateCache.get("views/directives.chosekeyword.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.chosedKeywords.length > 0) {
                                        if ($scope.chosedKeywords.length < 2) {
                                            helper.myAlert("选取的关键词不足2个");
                                            return false;
                                        }

                                        if ($scope.chosedKeywords.length > 5) {
                                            helper.myAlert("选取的关键词不能超过5个");
                                            return false;
                                        }

                                        console.log(
                                            "$scope.chosedKeywords",
                                            $scope.chosedKeywords
                                        );
                                        // return false;
                                        $scope.keywordCheck({
                                            keyword: {
                                                tid: $scope.ngModel,
                                                title: $scope.titles,
                                                chosed_keywords: $scope.chosedKeywords
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }

                    //请求数据
                    $scope.pageData = function() {
                        var query = {
                            tid: $scope.ngModel
                        };

                        return dataService.keywordLibraryGet(query).then(function(data) {
                            console.error("data", data);
                            // return false;
                            if (data.Msg == "ok") {
                                $scope.titles = data.title;
                                $scope.keyword_list = data.keyword_list;
                            } else {
                                helper.myAlert("error：" + data.Msg);
                                return false;
                            }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.keywordlibrary.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("keywordLibrary", function(
        $templateCache,
        $compile,
        $timeout,
        helper
    ) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}'  ng-click='addItem();'>+{{'关键词库' | translate}}</a> ",
            scope: {
                appId: "=",
                ngModel: "=",
                title: "=",
                keywordCheck: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.titles = $scope.title ? $scope.title : "关键词库";
                    $scope.addItem = function() {
                        $scope.pageData().then(function(result) {
                            if (result) {
                                $scope.showPop();
                            }
                        });
                    };

                    $scope.showPop = function() {
                        $scope.chosedKeywords = [];
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: "{{titles | translate}}",
                            body: $templateCache.get("views/directives.keywordlibrary.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.chosedKeywords.length > 0) {
                                        if ($scope.chosedKeywords.length < 2) {
                                            helper.myAlert("选取的关键词不足2个");
                                            return false;
                                        }

                                        if ($scope.chosedKeywords.length > 5) {
                                            helper.myAlert("选取的关键词不能超过5个");
                                            return false;
                                        }

                                        console.log(
                                            "$scope.chosedKeywords",
                                            $scope.chosedKeywords
                                        );
                                        // return false;
                                        $scope.keywordCheck({
                                            keyword: {
                                                tid: $scope.ngModel,
                                                title: $scope.titles,
                                                chosed_keywords: $scope.chosedKeywords
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }

                    //请求数据
                    $scope.pageData = function() {
                        if (!$scope.appId) {
                            helper.myAlert("没有选择小程序，无法查看关键词库！");
                            return false;
                        }
                        var query = {
                            app_id: $scope.appId,
                            tid: $scope.ngModel
                        };

                        return dataService.keywordGet(query).then(function(data) {
                            if (data.Msg == "ok") {
                                // $scope.titles = data.title;
                                $scope.keyword_list = data.keyword_list;
                                return true;
                            } else {
                                helper.myAlert("error：" + data.Msg);
                                return false;
                            }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.sendmbmsg.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("sendMbMsg", function(
        $templateCache,
        $compile,
        $timeout,
        helper
    ) {
        return {
            restrict: "EA",
            template: "<button class='sui-btn btn-primary'  ng-click='addItem();'>{{'发送消息' | translate}}</button> ",
            scope: {
                title: "=",
                userId: "=",
                seller: "=",
                chosedType: "=",
                appId: "=",
                page: "=",
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.chosedType = $scope.chosedType ? $scope.chosedType : "sms";
                    //                     console.error("chosedType",$scope.chosedType);
                    $scope.titles = $scope.title ? $scope.title : "发送短信";
                    $scope.msgData = {};
                    $scope.addItem = function() {
                        if (!$scope.userId) {
                            helper.myAlert("用户id为空，不能发送短信");
                            return false;
                        } else {
                            if ($scope.seller) {
                                showPop();
                            } else {
                                $scope.pageData().then(showPop);
                            }
                        }
                    };

                    function showPop() {
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: "{{titles | translate}}",
                            body: $templateCache.get("views/directives.sendmbmsg.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 300,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if (!$scope.msgData.content) {
                                        helper.myAlert("短信内容不能为空");
                                        return false;
                                    }

                                    if ($scope.chosedType == "sms") {
                                        if (!$scope.msgData.mobile) {
                                            helper.myAlert("手机号码不能空");
                                            return false;
                                        }
                                        $scope.sendMbMsg(
                                            $scope.msgData.mobile,
                                            $scope.msgData.content
                                        );
                                    } else if ($scope.chosedType == "template") {
                                        $scope.sendTemplateMsg($scope.msgData.content);
                                    }
                                });
                            }
                        });
                    }

                    //请求数据
                    $scope.pageData = function() {
                        var query = {
                            query: {
                                user_id: $scope.userId
                            }
                        };
                        return dataService.sellerGet(query).then(function(data) {
                            if (data.users) {
                                $scope.userData = data.users[0];
                                if (!$scope.userData.mobile) {
                                    $scope.msgData.mobile = "";
                                } else {
                                    $scope.msgData.mobile = $scope.userData.mobile;
                                }
                            } else {
                                helper.myAlert("error：" + data.errmsg);
                                return false;
                            }
                        });
                    };

                    //请求发送短信
                    $scope.sendMbMsg = function(mobile, content) {
                        var data = {
                            user_id: $scope.userId,
                            mobile: mobile,
                            content: content
                        };

                        dataService.sendMbMsg(data).then(function(result) {
                            if (result.Msg == "ok") {
                                helper.myAlert("短信发送成功，查看日志");
                            } else {
                                helper.myAlert("error:" + result.Msg);
                                return false;
                            }
                        });
                    };

                    //发送模板消息
                    $scope.sendTemplateMsg = function(content) {
                        var page = $scope.page ? $scope.page : "";
                        page = page.replace("[msg]", encodeURIComponent(content));
                        var data = {
                            user_id: $scope.userId,
                            store_id: $rootScope.store_id,
                            app_id: $scope.appId,
                            content: content,
                            page: page
                        };

                        dataService.sendTemplateMsg(data).then(function(result) {
                            if (result.Msg == "ok") {
                                helper.rmAlert();
                                helper.message("发送成功！");
                            } else {
                                helper.myAlert("error:" + result.Msg);
                                return false;
                            }
                        });
                    };
                }
            ],
            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.chooselevel.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("chooseLevel", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}' ng-click='addItem();'>+{{'选择会员等级' | translate}}</a> ",
            scope: {
                ngModel: "=",
                mult: "@",
                outHtml: "@",
                backcall: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                "helper",
                function($rootScope, $scope, dataService, helper) {
                    $scope.ismult = $scope.mult == "yes" ? true : false;

                    $scope.addItem = function() {
                        $scope.pageData().then(showPop);
                    };

                    function showPop() {
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"会员等级" | translate}}',
                            body: $templateCache.get("views/directives.chooselevel.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.ismult) {
                                        if ($scope.chosedItems.length > 0) {
                                            $scope.ngModel = $scope.chosedItems;
                                        }
                                    } else {
                                        if ($scope.levelInfo) {
                                            $scope.ngModel = $scope.levelInfo.member_level_id;
                                            if ($scope.backcall) {
                                                $scope.backcall({
                                                    member_level_id: $scope.ngModel
                                                });
                                            }
                                        }
                                    }
                                });
                            }
                        });
                    }

                    $scope.isChecked = function(info) {
                        $scope.levelInfo = info;
                    };

                    //请求数据
                    $scope.itemsTotal = 0;
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        var query = {
                            content: {
                                page_no: pageNo,
                                page_size: $scope.pageSize
                            }
                        };
                        return dataService.memberlevelGet(query).then(function(list) {
                            if (list) {
                                $scope.member_levels = list.member_levels;
                                $scope.total_count = list.total_count;
                            }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {
                if (scope.outHtml) {} else {
                    scope.class = attrs.xclass;
                }
            }
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.choserole.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("choseRole", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            template: '<a href="javascript:void(0);" class="{{class}}" ng-click="addItem();">+{{titles | translate}}</a><span style="padding-left: 5px;" ng-if="role_name">当前角色：{{role_name}}</span>',
            scope: {
                ngModel: "=",
                roleCheck: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.titles = $scope.title ? $scope.title : "选择角色";
                    if ($scope.ngModel) {
                        var query = {};
                        query.query = {
                            role_id: $scope.ngModel
                        };
                        dataService.roleGet(query).then(function(list) {
                            if (list.roles.length > 0) {
                                $scope.role_name = list.roles[0].name;
                            }
                        });
                    } else {
                        $scope.role_name = "无";
                    }

                    $scope.addItem = function() {
                        $scope.pageData().then(showPop);
                    };

                    function showPop() {
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: "{{titles | translate}}",
                            body: $templateCache.get("views/directives.choserole.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.roleInfo) {
                                        if ($scope.roleCheck) {
                                            $scope.roleCheck({
                                                roleInfo: $scope.roleInfo
                                            });
                                        }
                                    }
                                });
                            }
                        });
                    }

                    $scope.isChecked = function(role) {
                        $scope.roleInfo = role;
                    };

                    //请求数据
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        var query = {
                            page_no: pageNo ? pageNo : "1",
                            page_size: $scope.pageSize
                        };
                        return dataService.roleGet(query).then(function(data) {
                            if (data.total_count) {
                                $scope.role_list = data.roles;
                                $scope.total_count = data.total_count;
                            }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .run([
        "$templateCache",
        "dataService",
        function($templateCache, dataService) {
            var tplName = "views/directives.choseroleuser.html";
            dataService.tpl(tplName).then(function(html) {
                $templateCache.put(tplName, html);
            });
        }
    ])

    .directive("choseRoleUser", function($templateCache, $compile, $timeout) {
        return {
            restrict: "EA",
            template: "<a href='javascript:void(0);' class='{{class}}' ng-click='addItem();'>+{{titles | translate}}</a> ",
            scope: {
                roleId: "=",
                userCheck: "&"
            },
            controller: [
                "$rootScope",
                "$scope",
                "dataService",
                function($rootScope, $scope, dataService) {
                    $scope.titles = $scope.title ? $scope.title : "选择用户";
                    $scope.addItem = function() {
                        if (!$scope.roleId) {
                            helper.myAlert("没有选择角色，无法继续选择用户");
                            return false;
                        } else {
                            $scope.pageData().then(showPop);
                        }
                    };

                    function showPop() {
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: "{{titles | translate}}",
                            body: $templateCache.get("views/directives.choseroleuser.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "large",
                            height: 400,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.userInfo) {
                                        if ($scope.userCheck) {
                                            $scope.userCheck({
                                                userInfo: $scope.userInfo
                                            });
                                        }
                                    }
                                });
                            }
                        });
                    }

                    $scope.isChecked = function(user) {
                        $scope.userInfo = user;
                    };

                    //请求数据
                    $scope.pageSize = 10;
                    $scope.pageData = function(pageNo) {
                        var query = {
                            page_no: pageNo ? pageNo : "1",
                            page_size: $scope.pageSize,
                            role_id: $scope.roleId
                        };
                        return dataService.subuserGet(query).then(function(data) {
                            if (data.total_count) {
                                $scope.user_list = data.users;
                                $scope.total_count = data.total_count;
                            }
                        });
                    };
                }
            ],

            link: function(scope, element, attrs, ctrl) {}
        };
    })

    .directive("toggleSwitch", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                templateUrl: "views/directives.toggleswitch.html",
                scope: {
                    status: "=",
                    toggleCb: "&",
                    isnum: "@"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.toggle = {};
                        $scope.toggle.switch = $scope.status == 0 ? false : true;

                        //开关切换 状态
                        $scope.switch = function() {
                            if ($scope.isnum) {
                                if ($scope.toggle.switch) {
                                    $scope.status = 1
                                } else {
                                    $scope.status = 0;
                                }
                            }

                            //如果有回调函数的话，把状态值传回去
                            if ($scope.toggleCb) {
                                $scope.toggleCb({
                                    status: $scope.toggle.switch
                                });
                            }
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.choseitemlabel.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("choseItemLabel", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                templateUrl: "views/directives.choseitemlabel.html",
                // template: "<span><a href='javascript:;' class='sui-btn btn-link' ng-click='showPro()'>+ {{'选择标签'|translate}}</a>",
                scope: {
                    ngModel: "=",
                    itemid: "=",
                    type: "@"
                        // callback: "@"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        function getLabelList(pageNo) {
                            //                             console.error("$scope.itemid", $scope.itemid);
                            $scope.ngModel = _.isArray($scope.ngModel) ? $scope.ngModel : [];
                            var query = {
                                page_no: 1,
                                page_size: 200,
                                store_id: $rootScope.store_id
                            };
                            dataService.labelGet(query).then(function(list) {
                                if (list) {
                                    $scope.totalMenu = list.label_list;
                                    if ($scope.itemid) {
                                        $scope.getrelation();
                                    }
                                }
                            });
                        }

                        //查询绑定关系
                        $scope.getrelation = function() {
                            //                             console.error("$scope.type", $scope.type);
                            var query;
                            if (!$scope.type) {
                                query = {
                                    item_id: $scope.itemid
                                };
                            } else {
                                query = {
                                    cat_id: $scope.itemid
                                };
                            }
                            query.page_size = "100";
                            var api = $scope.type ?
                                "catLabelRelationGet" :
                                "itemLabelRelationGet";
                            dataService[api](query).then(function(list) {
                                if (list) {
                                    $scope.relationData = list.relation_list;
                                    _.map(list.relation_list, function(dd) {
                                        if (dd.label_id) {
                                            $scope.ngModel.push({
                                                label_id: dd.label_id
                                            });
                                        }
                                    });
                                }
                            });
                        };

                        //选择
                        $scope.check = function(data, type) {
                            console.error("type", type, data);
                            if (!type && $scope.itemid) {
                                if ($scope.relationData) {
                                    var api = $scope.type ?
                                        "catlabelrelationDel" :
                                        "itemlabelrelationDel";
                                    _.map($scope.relationData, function(da) {
                                        if (data.label_id == da.label_id) {
                                            var del = {
                                                relation_id: da.relation_id
                                            };
                                            dataService[api](del).then(function(result) {});
                                        }
                                    });
                                }
                            }
                        };

                        getLabelList();
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.batchlabel.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("batchLabel", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: "<span><a href='javascript:;' class='sui-btn btn-success' ng-click='init()'>绑定标签</a>",
                scope: {
                    callback: "&"
                },
                controller: ["$rootScope", "$scope", "dataService", function($rootScope, $scope, dataService) {
                    $scope.init = function() {
                        $scope.ngModel = [];
                        console.error("1111111111111111111");
                        getLabelList().then(showPop);
                    };

                    function showPop() {
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"标签" | translate}}',
                            body: $templateCache.get("views/directives.batchlabel.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "normal",
                            height: 200,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.callback) {
                                        $scope.callback({
                                            data: $scope.ngModel
                                        });
                                    }
                                });
                            }
                        });
                    }

                    function getLabelList(pageNo) {
                        var query = {
                            page_no: 1,
                            page_size: 100,
                            store_id: $rootScope.store_id
                        };
                        return dataService.labelGet(query).then(function(list) {
                            if (list) {
                                $scope.totalMenu = list.label_list;
                                console.error("$scope.totalMenu", $scope.totalMenu);
                                return $scope.totalMenu;
                            }
                        });
                    }
                }],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .directive("batchItmeStype", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: "<span><a href='javascript:;' class='sui-btn btn-success' ng-click='showpop()'>绑定类型</a>",
                scope: {
                    callback: "&"
                },
                controller: ["$rootScope", "$scope", "dataService", function($rootScope, $scope, dataService) {

                    $scope.showpop = function() {

                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"商品类型" | translate}}',
                            body: '<div>选择类型:<item-type ng-model="ngModel" dft="1"></item-type></div>',
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "normal",
                            height: 200,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.callback) {
                                        $scope.callback({
                                            data: $scope.ngModel
                                        });
                                    }
                                });
                            }
                        });
                    }
                }],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.batchfreight.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("batchFreight", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: "<span><a href='javascript:;' class='sui-btn btn-success' ng-click='init()'>批量设置运费模板</a>",
                scope: {
                    callback: "&"
                },
                controller: ["$rootScope", "$scope", "dataService", function($rootScope, $scope, dataService) {
                    $scope.init = function() {
                        $scope.ngModel = 0;
                        getList().then(showPop);
                    };

                    function showPop() {
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"标签" | translate}}',
                            body: $templateCache.get("views/directives.batchfreight.html"),
                            okBtn: '{{"ok" | translate}}',
                            closeBtn: true,
                            transition: true,
                            width: "normal",
                            height: 200,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                                return true;
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if ($scope.callback) {
                                        $scope.callback({
                                            data: $scope.ngModel
                                        });
                                    }
                                });
                            }
                        });
                    }

                    //单选
                    $scope.checkedRadio = function(id) {
                        if (id) {
                            $scope.ngModel = id;
                            console.error("$scope.ngModel", $scope.ngModel);
                        }
                    };

                    function getList(pageNo) {
                        var query = {
                            page_no: pageNo ? pageNo : "1",
                            page_size: $scope.pageSize,
                            order_by: "fare_id:desc"
                        };
                        query.query = {
                            "store_id": $rootScope.store_id
                        };

                        return dataService.postfeeGet(query, function(list) {
                            if (list) {
                                $scope.fare_temp = list.fare_temp;
                                $scope.total_count = list.total_count;
                            }
                        });
                    }




                }],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.customsitems.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("choiceCustomsItems", function(
            $templateCache,
            $compile,
            $timeout,
            helper
        ) {
            return {
                restrict: "EA",
                template: '  <a href="javascript:;" class="sui-btn btn-success btn-small" style="margin:5px;" ng-click="getCode()" >关联海关备案商品</a> ',
                scope: {
                    skuCode: "=",
                    barcode: "=",
                    // taxrate: "=",
                    del: "="
                        // backcall: "&",
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    "$timeout",
                    "helper",
                    function($rootScope, $scope, dataService) {
                        $scope.getCode = function() {
                            if (!$scope.del || $scope.del.length > 1) {
                                helper.myAlert("只有绑定一个发货点！才能操作备案商品！");
                                return false;
                            } else {
                                $scope.pageData().then(function(data) {
                                    if (data) {
                                        showPop();
                                    }
                                });
                            }
                        };

                        function showPop() {
                            $scope.chosedItems = $scope.chosedItems ? $scope.chosedItems : [];
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: '{{"Item" | translate}}',
                                body: $templateCache.get("views/directives.customsitems.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.checkItem) {
                                            $scope.skuCode = $scope.checkItem.SKU;
                                            $scope.barcode = $scope.checkItem.BAR_CODE;
                                            $scope.taxrate = $scope.checkItem.tax_rate;
                                        }
                                    });
                                }
                            });
                        }
                        // 选择
                        $scope.isChecked = function(item) {
                            //                             console.error("item,item", item);
                            if (item) {
                                $scope.checkItem = item;
                            }
                        };
                        //请求数据
                        $scope.pageSize = 10;
                        $scope.pageData = function(pageNo) {
                            var query = {
                                page_no: pageNo ? pageNo : "1",
                                page_size: $scope.pageSize,
                                del_id: $scope.del[0],
                                company_id: $rootScope.s_partner_id
                            };
                            if ($scope.keyword) {
                                query.keyword = $scope.keyword;
                            }
                            if ($scope.SKU) {
                                query.SKU = $scope.SKU;
                            }
                            return dataService.customsItemsGet(query).then(function(data) {
                                if (data.total_count) {
                                    $scope.items = data.list;
                                    $scope.total_count = data.total_count;
                                    return $scope.items;
                                } else {
                                    if (!data.Msg && data.total_count == 0) {
                                        return $scope.items = [];
                                    } else {
                                        helper.myAlert(data.Msg);
                                        return false;
                                    }
                                }
                            });
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("map", function($templateCache, $compile, $timeout, helper) {
            return {
                restrict: "EA",
                template: '<div><input type="text" placeholder="搜索地区" ng-model="searchLocationName" /> <button class="sui-btn btn-success" ng-click="searchName(searchLocationName)">搜索地址</button></div>  <div ng-init="initmap()" id="qqMap" style="width:500px; height:270px"></div>',
                scope: {
                    callback: "&",
                    lat: "=",
                    lng: "=",
                    // backcall: "&",
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    "$timeout",
                    "helper",
                    function($rootScope, $scope, dataService) {
                        var marker = null;
                        var geocoder = null;
                        var geocoderStatus = 0;
                        var localtion = null;
                        var address = null;
                        var mapObj = null;

                        $scope.initmap = function() {
                            $scope.lat = $scope.lat ? $scope.lat : "39.916527";
                            $scope.lng = $scope.lng ? $scope.lng : "116.397128";
                            console.error("8888888", $scope.lat, $scope.lng);
                            mapObj = new qq.maps.Map(document.getElementById("qqMap"));
                            mapObj.panTo(new qq.maps.LatLng($scope.lat, $scope.lng));
                            mapObj.zoomTo(12);
                            qq.maps.event.addListener(mapObj, "click", function(event) {
                                var center = new qq.maps.LatLng(
                                    event.latLng.getLat(),
                                    event.latLng.getLng()
                                );
                                geocoderStatus = 1;
                                if (!marker) {
                                    marker = new qq.maps.Marker({
                                        position: center,
                                        map: mapObj
                                    });
                                } else {
                                    marker.setMap(null);
                                    marker = new qq.maps.Marker({
                                        position: center,
                                        map: mapObj
                                    });
                                }
                                geocoder.getAddress(center);
                            });
                            geocoder = new qq.maps.Geocoder({
                                complete: function(result) {
                                    console.error("result", result);
                                    if (geocoderStatus == 2) {
                                        mapObj.setCenter(result.detail.location);
                                    } else if (geocoderStatus == 1) {
                                        localtion = result.detail.location;
                                        address = result.detail.address;
                                    }
                                    var data = {
                                        localtion: localtion,
                                        address: address
                                    };
                                    if (address && localtion) {
                                        $scope.callback({
                                            data: data ? data : null
                                        });
                                    }
                                }
                            });
                        };
                        $scope.searchName = function(name) {
                            geocoderStatus = 2;
                            geocoder.getLocation(name);
                            console.error("mapName", $scope.searchLocationName);
                        };
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.vdesc.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("itemVdesc", function(
            $templateCache,
            $compile,
            $timeout,
            helper
        ) {
            return {
                restrict: "EA",
                template: ' <a href="javascript:;" class="sui-btn btn-success btn-small" ng-click="getVdesc()" >虚拟字段</a> ',
                scope: {
                    ngModel: "=",
                    // backcall: "&",
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    "$timeout",
                    "helper",
                    function($rootScope, $scope, dataService) {
                        $scope.getVdesc = function() {
                            showPop();
                        };

                        function showPop() {
                            $scope.v = random();
                            $scope.vdesc = $scope.ngModel ? $scope.ngModel : "";
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: '{{"虚拟字段" | translate}}',
                                body: $templateCache.get("views/directives.vdesc.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        if ($scope.vdesc) {
                                            $scope.ngModel = $scope.vdesc;
                                        }
                                    });
                                }
                            });
                        }
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.skuitemlist.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("skuItemList", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                templateUrl: "views/directives.skuitemlist.html",
                scope: {
                    outerid: "@",
                    skuid: "@",
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.initData = function(pageNo) {
                            var query = {
                                store_id: $rootScope.store_id
                            };
                            if ($scope.outerid) {
                                query.sku_outer_id = $scope.outerid;
                            }
                            if ($scope.skuid) {
                                query.sku_id = $scope.skuid;
                            }

                            dataService.getskulist(query, 1800).then(function(list) {
                                if (list.Msg) {

                                } else {
                                    $scope.item = list.data;
                                }
                            });
                        };
                        $scope.initData();
                    }
                ],

                link: function(scope, element, attrs, ctrl) {

                }
            };
        })
        .directive("skuTaxfee", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: '<div ng-show="errormsg">{{errormsg}}</div><div ng-show="texfee">清关价：{{xprice}}<br><font color="red">税率：{{tax_rate}}= (<span title="增值税">{{zz_rate}}</span> + <span title="消费税">{{xf_rate}}</span>)*0.7 </font><br>税款:{{texfee}}<br>HsCode：<a href="https://www.hsbianma.com/Code/{{hs_code}}.html" target="_blank">{{hs_code}}</a></div>',
                scope: {
                    outerid: "=",
                    price: "=",
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.initData = function() {
                            $scope.query = {
                                store_id: $rootScope.store_id,
                                price: $scope.price,
                                sku_outer_id: $scope.outerid
                            };
                            dataService.gettemtax($scope.query).then(function(list) {
                                if (!list.Msg) {
                                    $scope.texfee = list.tax;
                                    $scope.tax_rate = list.tax_rate;
                                    $scope.hs_code = list.hs_code;
                                    $scope.xprice = list.xprice;
                                    $scope.zz_rate = list.zz_rate;
                                    $scope.xf_rate = list.xf_rate;
                                    $scope.errormsg = false;
                                } else {
                                    $scope.errormsg = list.Msg;
                                }
                            });
                        };
                        $scope.initData();
                    }
                ],

                link: function(scope, element, attrs, ctrl) {
                    scope.$watch('price', function(newValue, oldValue, scope) {
                        if (newValue) {
                            //                             console.error("newValue", newValue);
                            scope.initData(); //初始化
                        }
                    }, true);
                    scope.$watch('outerid', function(newValue, oldValue, scope) {
                        if (newValue) {
                            //                             console.error("newValue", newValue);
                            scope.initData(); //初始化
                        }
                    }, true);
                }
            };
        })
        .directive("mainSkuFee", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: '<div style="color:red" ng-show="fee">底价:{{fee}}</div>',
                scope: {
                    outerid: "=",
                    pitemid: "=",
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.initData = function() {
                            if (!$scope.pitemid) {
                                return;
                            }

                            $scope.query = {
                                p_item_id: $scope.pitemid,
                                sku_outer_id: $scope.outerid
                            };
                            dataService.getmainskuprice($scope.query).then(function(list) {
                                if (!list.Msg) {
                                    $scope.fee = list.price;
                                }
                            });
                        };
                        $scope.initData();
                    }
                ],

                link: function(scope, element, attrs, ctrl) {
                    scope.$watch('price', function(newValue, oldValue, scope) {
                        if (newValue) {
                            console.error("newValue", newValue);
                            scope.initData(); //初始化
                        }
                    }, true);
                    scope.$watch('outerid', function(newValue, oldValue, scope) {
                        if (newValue) {
                            //                             console.error("newValue", newValue);
                            scope.initData(); //初始化
                        }
                    }, true);
                }
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.customeone.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("customeOne", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                templateUrl: "views/directives.customeone.html",
                scope: {
                    outerid: "=",
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.initData = function(pageNo) {
                            var query = {
                                SKU: $scope.outerid
                            };
                            dataService.customsItemsGet(query).then(function(list) {
                                if (list) {
                                    $scope.item = list.list[0];
                                }
                            });
                        };
                        $scope.initData();
                    }
                ],

                link: function(scope, element, attrs, ctrl) {
                    scope.$watch('outerid', function(newValue, oldValue, scope) {
                        if (newValue) {
                            scope.initData(); //初始化
                        }
                    });
                }
            };
        })
        .directive("itemTitle", function($templateCache, $compile, $timeout) {
            return {
                restrict: "EA",
                template: '<span ng-show="title">{{title}}</span>',
                scope: {
                    itemid: "=",
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    function($rootScope, $scope, dataService) {
                        $scope.initData = function() {
                            var query = {
                                query: {
                                    item_id: $scope.itemid
                                }
                            };
                            dataService.itemGet(query).then(function(list) {
                                if (list.total_count > 0) {
                                    $scope.itemData = list.items[0];
                                    $scope.title = $scope.itemData['title'];
                                }
                            });
                        };
                        $scope.initData();
                    }
                ],
                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.idcard.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("idCard", function(
            $templateCache,
            $compile,
            $timeout,
            helper
        ) {
            return {
                restrict: "EA",
                template: '<a href="javascript:;" class="sui-btn btn-success btn-small" ng-click="getVdesc()">身份证</a>',
                scope: {
                    idNumber: "=",
                    idName: "=",
                    orderid: "="
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    "$timeout",
                    "helper",
                    function($rootScope, $scope, dataService) {
                        if ($scope.idNumber && $scope.idName) {
                            $scope.id_number = $scope.idNumber;
                            $scope.id_name = $scope.idName;
                        }
                        $scope.getVdesc = function() {
                            showPop();
                        };

                        function showPop() {
                            $scope.v = random();
                            $scope.vdesc = $scope.ngModel ? $scope.ngModel : "";
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: '{{"填写身份证" | translate}}',
                                body: $templateCache.get("views/directives.idcard.html"),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        console.error($scope.id_number, $scope.id_name);

                                        if (!$scope.id_number || !$scope.id_name) {
                                            helper.message("身份证姓名或者身份证号码必须填写！");
                                            return false;
                                        }

                                        var data = {
                                            "order_id": $scope.orderid,
                                            "id_name": $scope.id_name,
                                            "id_number": $scope.id_number
                                        };

                                        dataService.updatedIdcard(data).then(function(res) {
                                            if (res.Msg == "ok") {
                                                helper.message("身份证更新成功");
                                                $scope.idNumber = $scope.id_number;
                                                $scope.idName = $scope.id_name;
                                            } else {
                                                helper.message(res.Msg, "error");
                                                return false;
                                            }
                                        });
                                    });
                                }
                            });
                        }
                    }
                ],

                link: function(scope, element, attrs, ctrl) {}
            };
        })
        .run([
            "$templateCache",
            "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.marking.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive("marking", function(
            $templateCache,
            $compile,
            $timeout,
            helper
        ) {
            return {
                restrict: "EA",
                template: "<a href='javascript:void(0);' class='sui-btn btn-primary' id='btn-{{orderId}}'  ng-click='addItem();'>{{title}}</a> ",
                scope: {
                    title: "@",
                    orderId: "=",
                    upFlag: "&"
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    "helper",
                    function($rootScope, $scope, dataService) {
                        $scope.addItem = function() {
                            $scope.pageData().then(showPop);
                        };

                        function showPop() {
                            $.alert({
                                backdrop: "static",
                                keyboard: true,
                                title: "{{title | translate}}",
                                body: $templateCache.get(
                                    "views/directives.marking.html"
                                ),
                                okBtn: '{{"ok" | translate}}',
                                closeBtn: true,
                                transition: true,
                                width: "large",
                                height: 400,
                                show: function(e) {
                                    $compile($(e.currentTarget).contents())($scope);
                                    return true;
                                },
                                okHide: function(e) {
                                    $timeout(function() {
                                        $scope.addData();
                                    });
                                }
                            });
                        }

                        //请求数据
                        $scope.pageSize = 10;
                        $scope.pageData = function() {
                            return dataService.orderOne($scope.orderId).then(function(list) {
                                if (list) {
                                    $scope.flag = list.flag;
                                    $scope.remark = list.remark;
                                }
                            });
                        };

                        $scope.addData = function() {
                            if (!$scope.flag) {
                                helper.myAlert("error:标记颜色没有选择，无法对订单进行打标！");
                                return false;
                            }

                            var data = {
                                "order_id": $scope.orderId,
                                "flag": $scope.flag,
                                "remark": $scope.remark,
                            };

                            dataService.upOrderFlag(data).then(function(result) {
                                if (result.Msg == "ok") {
                                    helper.message($scope.orderId + ":订单打标成功");
                                    $scope.upFlag();
                                } else {
                                    helper.myTip(
                                        "error:" + result.Msg,
                                        "#btn-" + $scope.orderId
                                    );
                                    return false;
                                }
                            });
                        }
                    }
                ],

                link: function(scope, element, attrs, ctrl) {
                    scope.class = attrs.xclass;
                }
            };
        })
        .directive("markingInfo", function(
            $templateCache,
            $compile,
            $timeout,
            helper
        ) {
            return {
                restrict: "EA",
                templateUrl: "views/directives.marking.info.html",
                scope: {
                    flag: "=",
                    remark: "=",
                    orderId: "="
                },
                controller: [
                    "$rootScope",
                    "$scope",
                    "dataService",
                    "helper",
                    function($rootScope, $scope, dataService) {
                        $scope.addItem = function() {
                            $scope.pageData().then(showPop);
                        };

                        $scope.tip = function() {
                            if ($scope.remark) {
                                helper.myTip($scope.remark, "#tip-" + $scope.orderId);
                            }
                        }

                    }
                ],

                link: function(scope, element, attrs, ctrl) {
                    scope.class = attrs.xclass;
                }
            };
        })
})();